-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (44 loc) · 1.19 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
package main
import (
"github.com/chrislonng/starx"
"github.com/chrislonng/starx/component"
"github.com/chrislonng/starx/serialize/json"
"github.com/chrislonng/starx/session"
"net/http"
"github.com/chrislonng/starx/log"
)
type Room struct {
component.Base
channel *starx.Channel
}
type UserMessage struct {
Name string `json:"name"`
Content string `json:"content"`
}
type JoinResponse struct {
Code int `json:"code"`
Result string `json:"result"`
}
func NewRoom() *Room {
return &Room{
channel: starx.ChannelService.NewChannel("room"),
}
}
func (r *Room) Join(s *session.Session, msg []byte) error {
s.Bind(s.ID) // binding session uid
r.channel.Add(s) // add session to channel
return s.Response(JoinResponse{Result: "sucess"})
}
func (r *Room) Message(s *session.Session, msg *UserMessage) error {
return r.channel.Broadcast("onMessage", msg)
}
func main() {
starx.SetAppConfig("configs/app.json")
starx.SetServersConfig("configs/servers.json")
starx.Register(NewRoom())
starx.SetServerID("demo-server-1")
starx.SetSerializer(json.NewJsonSerializer())
log.SetLevel(log.LevelDebug)
starx.SetCheckOriginFunc(func(_ *http.Request) bool { return true })
starx.Run()
}