-
Notifications
You must be signed in to change notification settings - Fork 465
/
connector.go
87 lines (75 loc) · 2.02 KB
/
connector.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
package services
import (
"context"
"fmt"
"github.com/topfreegames/pitaya"
"github.com/topfreegames/pitaya/component"
"github.com/topfreegames/pitaya/examples/demo/protos"
)
// ConnectorRemote is a remote that will receive rpc's
type ConnectorRemote struct {
component.Base
}
// Connector struct
type Connector struct {
component.Base
}
// SessionData struct
type SessionData struct {
Data map[string]interface{}
}
// Response struct
type Response struct {
Code int32
Msg string
}
func reply(code int32, msg string) (*Response, error) {
res := &Response{
Code: code,
Msg: msg,
}
return res, nil
}
// GetSessionData gets the session data
func (c *Connector) GetSessionData(ctx context.Context) (*SessionData, error) {
s := pitaya.GetSessionFromCtx(ctx)
res := &SessionData{
Data: s.GetData(),
}
return res, nil
}
// SetSessionData sets the session data
func (c *Connector) SetSessionData(ctx context.Context, data *SessionData) (*Response, error) {
s := pitaya.GetSessionFromCtx(ctx)
err := s.SetData(data.Data)
if err != nil {
return nil, pitaya.Error(err, "CN-000", map[string]string{"failed": "set data"})
}
return reply(200, "success")
}
// NotifySessionData sets the session data
func (c *Connector) NotifySessionData(ctx context.Context, data *SessionData) {
s := pitaya.GetSessionFromCtx(ctx)
err := s.SetData(data.Data)
if err != nil {
fmt.Println("got error on notify", err)
}
}
// SendPushToUser sends a push to a user
func (c *Connector) SendPushToUser(ctx context.Context, msg *UserMessage) (*Response, error) {
_, err := pitaya.SendPushToUsers("onMessage", msg, []string{"2"}, "connector")
if err != nil {
return nil, err
}
return &Response{
Code: 200,
Msg: "boa",
}, nil
}
// RemoteFunc is a function that will be called remotely
func (c *ConnectorRemote) RemoteFunc(ctx context.Context, msg *protos.RPCMsg) (*protos.RPCRes, error) {
fmt.Printf("received a remote call with this message: %s\n", msg)
return &protos.RPCRes{
Msg: fmt.Sprintf("received msg: %s", msg.GetMsg()),
}, nil
}