forked from GoBelieveIO/im_service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
264 lines (212 loc) · 6.82 KB
/
message.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* Copyright (c) 2014-2015, GoBelieve
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import "bytes"
import "encoding/binary"
//接入服务器消息
const MSG_AUTH_STATUS = 3
const MSG_ACK = 5
const MSG_PING = 13
const MSG_PONG = 14
const MSG_AUTH_TOKEN = 15
const MSG_RT = 17
const MSG_ENTER_ROOM = 18
const MSG_LEAVE_ROOM = 19
const MSG_ROOM_IM = 20
const MSG_UNREAD_COUNT = 22
//persistent, deprecated
const MSG_CUSTOMER_SERVICE_ = 23
//客户端->服务端
const MSG_SYNC = 26 //同步消息
//服务端->客服端
const MSG_SYNC_BEGIN = 27
const MSG_SYNC_END = 28
//通知客户端有新消息
const MSG_SYNC_NOTIFY = 29
//客户端->服务端
const MSG_SYNC_GROUP = 30//同步超级群消息
//服务端->客服端
const MSG_SYNC_GROUP_BEGIN = 31
const MSG_SYNC_GROUP_END = 32
//通知客户端有新消息
const MSG_SYNC_GROUP_NOTIFY = 33
//客服端->服务端,更新服务器的synckey
const MSG_SYNC_KEY = 34
const MSG_GROUP_SYNC_KEY = 35
//系统通知消息, unpersistent
const MSG_NOTIFICATION = 36
//消息的meta信息
const MSG_METADATA = 37
//im实例使用
const MSG_PENDING_GROUP_MESSAGE = 251
//服务器消息, 被所有服务器使用
//persistent 点对点消息
const MSG_IM = 4
//persistent
const MSG_GROUP_NOTIFICATION = 7
const MSG_GROUP_IM = 8
//persistent
const MSG_SYSTEM = 21
//persistent, deprecated
const MSG_CUSTOMER_ = 24 //顾客->客服
const MSG_CUSTOMER_SUPPORT_ = 25 //客服->顾客
//persistent 不同app间的点对点消息
const MSG_CUSTOMER_V2 = 64
//路由服务器消息
const MSG_SUBSCRIBE = 130
const MSG_UNSUBSCRIBE = 131
const MSG_PUBLISH = 132
const MSG_PUSH = 134
const MSG_PUBLISH_GROUP = 135
const MSG_SUBSCRIBE_ROOM = 136
const MSG_UNSUBSCRIBE_ROOM = 137
const MSG_PUBLISH_ROOM = 138
func init() {
message_creators[MSG_GROUP_NOTIFICATION] = func()IMessage{return new(GroupNotification)}
message_creators[MSG_SYSTEM] = func()IMessage{return new(SystemMessage)}
message_creators[MSG_CUSTOMER_] = func()IMessage{return new(IgnoreMessage)}
message_creators[MSG_CUSTOMER_SUPPORT_] = func()IMessage{return new(IgnoreMessage)}
message_creators[MSG_CUSTOMER_V2] = func()IMessage{return new(CustomerMessageV2)}
vmessage_creators[MSG_GROUP_IM] = func()IVersionMessage{return new(IMMessage)}
vmessage_creators[MSG_IM] = func()IVersionMessage{return new(IMMessage)}
message_descriptions[MSG_IM] = "MSG_IM"
message_descriptions[MSG_GROUP_NOTIFICATION] = "MSG_GROUP_NOTIFICATION"
message_descriptions[MSG_GROUP_IM] = "MSG_GROUP_IM"
message_descriptions[MSG_SYSTEM] = "MSG_SYSTEM"
message_descriptions[MSG_CUSTOMER_] = "MSG_CUSTOMER"
message_descriptions[MSG_CUSTOMER_SUPPORT_] = "MSG_CUSTOMER_SUPPORT"
message_descriptions[MSG_CUSTOMER_V2] = "MSG_CUSTOMER_V2"
external_messages[MSG_IM] = true;
external_messages[MSG_GROUP_IM] = true;
external_messages[MSG_CUSTOMER_] = true;
external_messages[MSG_CUSTOMER_SUPPORT_] = true;
external_messages[MSG_CUSTOMER_V2] = true;
}
type GroupNotification struct {
notification string
}
func (notification *GroupNotification) ToData() []byte {
return []byte(notification.notification)
}
func (notification *GroupNotification) FromData(buff []byte) bool {
notification.notification = string(buff)
return true
}
type IMMessage struct {
sender int64
receiver int64
timestamp int32
msgid int32
content string
}
func (message *IMMessage) ToDataV0() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, message.sender)
binary.Write(buffer, binary.BigEndian, message.receiver)
binary.Write(buffer, binary.BigEndian, message.msgid)
buffer.Write([]byte(message.content))
buf := buffer.Bytes()
return buf
}
func (im *IMMessage) FromDataV0(buff []byte) bool {
if len(buff) < 20 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &im.sender)
binary.Read(buffer, binary.BigEndian, &im.receiver)
binary.Read(buffer, binary.BigEndian, &im.msgid)
im.content = string(buff[20:])
return true
}
func (message *IMMessage) ToDataV1() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, message.sender)
binary.Write(buffer, binary.BigEndian, message.receiver)
binary.Write(buffer, binary.BigEndian, message.timestamp)
binary.Write(buffer, binary.BigEndian, message.msgid)
buffer.Write([]byte(message.content))
buf := buffer.Bytes()
return buf
}
func (im *IMMessage) FromDataV1(buff []byte) bool {
if len(buff) < 24 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &im.sender)
binary.Read(buffer, binary.BigEndian, &im.receiver)
binary.Read(buffer, binary.BigEndian, &im.timestamp)
binary.Read(buffer, binary.BigEndian, &im.msgid)
im.content = string(buff[24:])
return true
}
func (im *IMMessage) ToData(version int) []byte {
if version == 0 {
return im.ToDataV0()
} else {
return im.ToDataV1()
}
}
func (im *IMMessage) FromData(version int, buff []byte) bool {
if version == 0 {
return im.FromDataV0(buff)
} else {
return im.FromDataV1(buff)
}
}
type SystemMessage struct {
notification string
}
func (sys *SystemMessage) ToData() []byte {
return []byte(sys.notification)
}
func (sys *SystemMessage) FromData(buff []byte) bool {
sys.notification = string(buff)
return true
}
type CustomerMessageV2 struct {
IMMessage
sender_appid int64
receiver_appid int64
}
func (im *CustomerMessageV2) ToData() []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, im.sender_appid)
binary.Write(buffer, binary.BigEndian, im.sender)
binary.Write(buffer, binary.BigEndian, im.receiver_appid)
binary.Write(buffer, binary.BigEndian, im.receiver)
binary.Write(buffer, binary.BigEndian, im.timestamp)
buffer.Write([]byte(im.content))
buf := buffer.Bytes()
return buf
}
func (im *CustomerMessageV2) FromData(buff []byte) bool {
if len(buff) < 36 {
return false
}
buffer := bytes.NewBuffer(buff)
binary.Read(buffer, binary.BigEndian, &im.sender_appid)
binary.Read(buffer, binary.BigEndian, &im.sender)
binary.Read(buffer, binary.BigEndian, &im.receiver_appid)
binary.Read(buffer, binary.BigEndian, &im.receiver)
binary.Read(buffer, binary.BigEndian, &im.timestamp)
im.content = string(buff[36:])
return true
}