Skip to content

Commit

Permalink
WIP - exchange protobuf information with the clients
Browse files Browse the repository at this point in the history
  • Loading branch information
felipejfc committed Mar 23, 2018
1 parent f93ae65 commit 9e07f41
Show file tree
Hide file tree
Showing 10 changed files with 743 additions and 35 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ run-chat-example:
run-cluster-example-frontend:
@go run examples/demo/cluster/main.go

run-cluster-example-frontend-protobuf:
@go run examples/demo/cluster_protobuf/main.go

run-cluster-example-backend:
@go run examples/demo/cluster/main.go --port 3251 --type room --frontend=false

Expand Down
2 changes: 0 additions & 2 deletions examples/demo/chat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ func main() {

//TODO need to fix that? pitaya.SetCheckOriginFunc(func(_ *http.Request) bool { return true })
ws := acceptor.NewWSAcceptor(":3250", "/pitaya")
tcp := acceptor.NewTCPAcceptor(":3255")
pitaya.AddAcceptor(ws)
pitaya.AddAcceptor(tcp)
pitaya.Configure(true, "chat", pitaya.Standalone)
pitaya.Start()
}
35 changes: 15 additions & 20 deletions examples/demo/cluster/services/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,27 @@ type Connector struct {
component.Base
}

// SessionData is the session data struct
type SessionData struct {
Data map[string]interface{} `json:"data"`
}

// GetSessionData gets the session data
func (c *Connector) GetSessionData(s *session.Session) (map[string]interface{}, error) {
return s.GetData(), nil
}

// SetSessionData sets the session data
func (c *Connector) SetSessionData(s *session.Session, data *SessionData) (string, error) {
err := s.SetData(data.Data)
if err != nil {
return "", err
}
return "success", nil
}

// NotifySessionData sets the session data
func (c *Connector) NotifySessionData(s *session.Session, data *SessionData) {
err := s.SetData(data.Data)
if err != nil {
fmt.Println("got error on notify", err)
}
}
//func (c *Connector) SetSessionData(s *session.Session, data *SessionData) (string, error) {
// err := s.SetData(data.Data)
// if err != nil {
// return "", err
// }
// return "success", nil
//}
//
//// NotifySessionData sets the session data
//func (c *Connector) NotifySessionData(s *session.Session, data *SessionData) {
// err := s.SetData(data.Data)
// if err != nil {
// fmt.Println("got error on notify", err)
// }
//}

// RemoteFunc is a function that will be called remotelly
func (c *ConnectorRemote) RemoteFunc(message string) (*RPCResponse, error) {
Expand Down
24 changes: 12 additions & 12 deletions examples/demo/cluster/services/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,18 @@ func (r *Room) GetSessionData(s *session.Session) (map[string]interface{}, error
return s.GetData(), nil
}

// SetSessionData sets the session data
func (r *Room) SetSessionData(s *session.Session, data *SessionData) (string, error) {
err := s.SetData(data.Data)
if err != nil {
return "", err
}
err = s.PushToFront()
if err != nil {
return "", err
}
return "success", nil
}
//// SetSessionData sets the session data
//func (r *Room) SetSessionData(s *session.Session, data *SessionData) (string, error) {
// err := s.SetData(data.Data)
// if err != nil {
// return "", err
// }
// err = s.PushToFront()
// if err != nil {
// return "", err
// }
// return "success", nil
//}

// Join room
func (r *Room) Join(s *session.Session) (*JoinResponse, error) {
Expand Down
84 changes: 84 additions & 0 deletions examples/demo/cluster_protobuf/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"flag"
"fmt"

"strings"

"github.com/topfreegames/pitaya"
"github.com/topfreegames/pitaya/acceptor"
"github.com/topfreegames/pitaya/cluster"
"github.com/topfreegames/pitaya/component"
"github.com/topfreegames/pitaya/examples/demo/cluster/services"
"github.com/topfreegames/pitaya/route"
"github.com/topfreegames/pitaya/serialize/protobuf"
"github.com/topfreegames/pitaya/session"
)

func configureBackend() {
room := services.NewRoom()
pitaya.Register(room,
component.WithName("room"),
component.WithNameFunc(strings.ToLower),
)

pitaya.RegisterRemote(room,
component.WithName("room"),
component.WithNameFunc(strings.ToLower),
)

// traffic stats
pitaya.AfterHandler(room.Stats.Outbound)
pitaya.BeforeHandler(room.Stats.Inbound)
}

func configureFrontend(port int) {
ws := acceptor.NewWSAcceptor(fmt.Sprintf(":%d", port), "/pitaya")
pitaya.Register(&services.Connector{},
component.WithName("connector"),
component.WithNameFunc(strings.ToLower),
)
pitaya.RegisterRemote(&services.ConnectorRemote{},
component.WithName("connectorremote"),
component.WithNameFunc(strings.ToLower),
)

pitaya.AddRoute("room", func(
session *session.Session,
route *route.Route,
servers map[string]*cluster.Server,
) (*cluster.Server, error) {
// will return the first server
for k := range servers {
return servers[k], nil
}
return nil, nil
})

pitaya.AddAcceptor(ws)
}

func main() {
port := flag.Int("port", 3250, "the port to listen")
svType := flag.String("type", "connector", "the server type")
isFrontend := flag.Bool("frontend", true, "if server is frontend")

flag.Parse()

defer (func() {
pitaya.Shutdown()
})()

pitaya.SetSerializer(protobuf.NewSerializer())
pitaya.SetServerType(*svType)

if !*isFrontend {
configureBackend()
} else {
configureFrontend(*port)
}

pitaya.Configure(*isFrontend, *svType, pitaya.Cluster)
pitaya.Start()
}
Loading

0 comments on commit 9e07f41

Please sign in to comment.