forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - exchange protobuf information with the clients
- Loading branch information
Showing
10 changed files
with
743 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.