forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (77 loc) · 2.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"context"
"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/json"
)
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),
)
}
func configureFrontend(port int) {
tcp := acceptor.NewTCPAcceptor(fmt.Sprintf(":%d", port))
pitaya.Register(&services.Connector{},
component.WithName("connector"),
component.WithNameFunc(strings.ToLower),
)
pitaya.RegisterRemote(&services.ConnectorRemote{},
component.WithName("connectorremote"),
component.WithNameFunc(strings.ToLower),
)
err := pitaya.AddRoute("room", func(
ctx context.Context,
route *route.Route,
payload []byte,
servers map[string]*cluster.Server,
) (*cluster.Server, error) {
// will return the first server
for k := range servers {
return servers[k], nil
}
return nil, nil
})
if err != nil {
fmt.Printf("error adding route %s\n", err.Error())
}
err = pitaya.SetDictionary(map[string]uint16{
"connector.getsessiondata": 1,
"connector.setsessiondata": 2,
"room.room.getsessiondata": 3,
"onMessage": 4,
"onMembers": 5,
})
if err != nil {
fmt.Printf("error setting route dictionary %s\n", err.Error())
}
pitaya.AddAcceptor(tcp)
}
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 pitaya.Shutdown()
pitaya.SetSerializer(json.NewSerializer())
if !*isFrontend {
configureBackend()
} else {
configureFrontend(*port)
}
pitaya.Configure(*isFrontend, *svType, pitaya.Cluster, map[string]string{})
pitaya.Start()
}