-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsg.go
129 lines (110 loc) · 3.04 KB
/
msg.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package grouplay
import (
"encoding/json"
"fmt"
"gopkg.in/igm/sockjs-go.v2/sockjs"
)
// Communicate protocal.
const (
CmdRegister = "register"
CmdCreateGroup = "create_group"
CmdExitGroup = "exit_group"
CmdJoinGroup = "join_group"
CmdGroupUpdate = "group_update"
CmdUpdateData = "update_data"
CmdStartGame = "start_game"
CmdGetData = "get_data"
CmdPlaying = "playing"
CmdPlayerAction = "player_action"
CmdStopGame = "stop_game"
CmdQuitGame = "quit_game"
CmdGameFinished = "game_finished"
CmdHostStop = "host_stop"
CmdGetGameList = "get_game_list"
CmdSpectateGame = "spectate_game"
CmdStopSpectating = "stop_spectating"
)
// Message format for transaction between server & client.
type Message struct {
Cmd string `json:"cmd"` // Message cmd
Msg string `json:"msg"` // Message body
Confirm bool `json:"confirm"` // Indicates if this message is a confirm message
}
type RegisterMessage struct {
ID string `json:"id"` //Old session id
Name string `json:"name"`
}
type CreateGroupMesssage struct {
Game string `json:"game"`
Max int `json:"max"`
AllowSpectator bool `json:"allowSpectator"`
}
type JoinGroupMessage struct {
GroupId string `json:"groupId"`
}
type ExitGroupMessage struct {
GroupId string `json:"groupId"`
}
type SpectateGroupMessage struct {
GroupId string `json:"groupId"`
}
type ErrorMessage struct {
Msg string `json:"msg"`
OK bool `json:"ok"`
}
type GroupListMessage struct {
Info *MyInfo `json:"myInfo"`
Joined *SimpleGroup `json:"joined"`
Waiting []*SimpleGroup `json:"waiting"`
Playing []*SimpleGroup `json:"playing"`
}
type StartGameMessage struct {
GroupId string `json:"groupId"`
}
type DataUpdateMessage struct {
Action string `json:"action"`
Data string `json:"data"`
}
func (m Message) String() string {
return fmt.Sprintf("%v %v %v", m.Cmd, m.Msg, m.Confirm)
}
// Send message back to client.
func Send(session sockjs.Session, cmd string, msg string, confirm bool) {
message := new(Message)
message.Cmd = cmd
message.Msg = msg
message.Confirm = confirm
SendMessage(session, message)
}
func SendMessage(session sockjs.Session, message *Message) {
bytes, err := json.Marshal(message)
if err != nil {
fmt.Println("send message error:", err)
return
}
json := string(bytes)
fmt.Println("Send message :", json)
SendJsonMessage(session, json)
}
func SendJsonMessage(session sockjs.Session, json string) {
session.Send(json)
}
func SendStructMessage(session sockjs.Session, cmd string, msg interface{}, confirm bool) {
bytes, err := json.Marshal(msg)
if err != nil {
fmt.Println("send message error:", err)
return
}
body := string(bytes)
Send(session, cmd, body, confirm)
}
func SendErrorMessage(session sockjs.Session, cmd string, msg string, ok bool, confirm bool) {
SendStructMessage(session, cmd, ErrorMessage{Msg: msg, OK: ok}, confirm)
}
func ToJson(msg interface{}) string {
bytes, err := json.Marshal(msg)
if err != nil {
return ""
}
return string(bytes)
}