-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathserve.go
467 lines (374 loc) · 10.5 KB
/
serve.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package go_ws
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/jianfengye/collection"
"go-gin-chat/models"
"go-gin-chat/services/helper"
"go-gin-chat/services/safe"
"go-gin-chat/ws"
"log"
"net/http"
"strconv"
"sync"
"time"
)
// 客户端连接详情
type wsClients struct {
Conn *websocket.Conn `json:"conn"`
RemoteAddr string `json:"remote_addr"`
Uid string `json:"uid"`
Username string `json:"username"`
RoomId string `json:"room_id"`
AvatarId string `json:"avatar_id"`
}
type msgData struct {
Uid string `json:"uid"`
Username string `json:"username"`
AvatarId string `json:"avatar_id"`
ToUid string `json:"to_uid"`
Content string `json:"content"`
ImageUrl string `json:"image_url"`
RoomId string `json:"room_id"`
Count int `json:"count"`
List []interface{} `json:"list"`
Time int64 `json:"time"`
}
// client & serve 的消息体
type msg struct {
Status int `json:"status"`
Data msgData `json:"data"`
Conn *websocket.Conn `json:"conn"`
}
type pingStorage struct {
Conn *websocket.Conn `json:"conn"`
RemoteAddr string `json:"remote_addr"`
Time int64 `json:"time"`
}
// 变量定义初始化
var (
wsUpgrader = websocket.Upgrader{}
clientMsg = msg{}
mutex = sync.Mutex{}
//rooms = [roomCount + 1][]wsClients{}
rooms = make(map[int][]interface{})
enterRooms = make(chan wsClients)
sMsg = make(chan msg)
offline = make(chan *websocket.Conn)
chNotify = make(chan int, 1)
pingMap []interface{}
clientMsgLock = sync.Mutex{}
clientMsgData = clientMsg // 临时存储 clientMsg 数据
)
// 定义消息类型
const msgTypeOnline = 1 // 上线
const msgTypeOffline = 2 // 离线
const msgTypeSend = 3 // 消息发送
const msgTypeGetOnlineUser = 4 // 获取用户列表
const msgTypePrivateChat = 5 // 私聊
const roomCount = 6 // 房间总数
type GoServe struct {
ws.ServeInterface
}
func (goServe *GoServe) RunWs(gin *gin.Context) {
// 使用 channel goroutine
Run(gin)
}
func (goServe *GoServe) GetOnlineUserCount() int {
return GetOnlineUserCount()
}
func (goServe *GoServe) GetOnlineRoomUserCount(roomId int) int {
return GetOnlineRoomUserCount(roomId)
}
func Run(gin *gin.Context) {
// @see https://github.com/gorilla/websocket/issues/523
wsUpgrader.CheckOrigin = func(r *http.Request) bool { return true }
c, _ := wsUpgrader.Upgrade(gin.Writer, gin.Request, nil)
defer c.Close()
done := make(chan struct{})
go read(c, done)
go write(done)
//for {
// select {
// case <-done:
// return
// }
//}
select {}
}
// HandelOfflineCoon 定时任务清理没有心跳的连接
func HandelOfflineCoon() {
objColl := collection.NewObjCollection(pingMap)
retColl := safe.Safety.Do(func() interface{} {
return objColl.Reject(func(obj interface{}, index int) bool {
nowTime := time.Now().Unix()
timeDiff := nowTime - obj.(pingStorage).Time
// log.Println("timeDiff", nowTime, obj.(pingStorage).Time, timeDiff)
if timeDiff > 60 { // 超过 60s 没有心跳 主动断开连接
offline <- obj.(pingStorage).Conn
return true
}
return false
})
}).(collection.ICollection)
interfaces, _ := retColl.ToInterfaces()
pingMap = interfaces
}
func appendPing(c *websocket.Conn) {
objColl := collection.NewObjCollection(pingMap)
// 先删除相同的
retColl := safe.Safety.Do(func() interface{} {
return objColl.Reject(func(obj interface{}, index int) bool {
if obj.(pingStorage).RemoteAddr == c.RemoteAddr().String() {
return true
}
return false
})
}).(collection.ICollection)
// 再追加
retColl = safe.Safety.Do(func() interface{} {
return retColl.Append(pingStorage{
Conn: c,
RemoteAddr: c.RemoteAddr().String(),
Time: time.Now().Unix(),
})
}).(collection.ICollection)
interfaces, _ := retColl.ToInterfaces()
pingMap = interfaces
}
func read(c *websocket.Conn, done chan<- struct{}) {
defer func() {
//捕获read抛出的panic
if err := recover(); err != nil {
log.Println("read发生错误", err)
//panic(nil)
}
}()
for {
_, message, err := c.ReadMessage()
//log.Println("client message", string(message), c.RemoteAddr())
if err != nil { // 离线通知
offline <- c
log.Println("ReadMessage error1", err)
c.Close()
close(done)
return
}
// 处理心跳响应 , heartbeat为与客户端约定的值
if string(message) == `heartbeat` {
appendPing(c)
chNotify <- 1
// log.Println("heartbeat pingMap:", pingMap)
c.WriteMessage(websocket.TextMessage, []byte(`{"status":0,"data":"heartbeat ok"}`))
<-chNotify
continue
}
json.Unmarshal(message, &clientMsgData)
clientMsgLock.Lock()
clientMsg = clientMsgData
clientMsgLock.Unlock()
//fmt.Println("来自客户端的消息", clientMsg, c.RemoteAddr())
if clientMsg.Data.Uid != "" {
if clientMsg.Status == msgTypeOnline { // 进入房间,建立连接
roomId, _ := getRoomId()
enterRooms <- wsClients{
Conn: c,
RemoteAddr: c.RemoteAddr().String(),
Uid: clientMsg.Data.Uid,
Username: clientMsg.Data.Username,
RoomId: roomId,
AvatarId: clientMsg.Data.AvatarId,
}
}
_, serveMsg := formatServeMsgStr(clientMsg.Status, c)
sMsg <- serveMsg
}
}
}
func write(done <-chan struct{}) {
defer func() {
//捕获write抛出的panic
if err := recover(); err != nil {
log.Println("write发生错误", err)
//panic(err)
}
}()
for {
select {
case <-done: // 当 done 通道关闭时,退出 write 函数
return
case r := <-enterRooms:
handleConnClients(r.Conn)
case cl := <-sMsg:
serveMsgStr, _ := json.Marshal(cl)
switch cl.Status {
case msgTypeOnline, msgTypeSend:
notify(cl.Conn, string(serveMsgStr))
case msgTypeGetOnlineUser:
chNotify <- 1
cl.Conn.WriteMessage(websocket.TextMessage, serveMsgStr)
<-chNotify
case msgTypePrivateChat:
chNotify <- 1
toC := findToUserCoonClient()
if toC != nil {
toC.(wsClients).Conn.WriteMessage(websocket.TextMessage, serveMsgStr)
}
<-chNotify
}
case o := <-offline:
disconnect(o)
}
}
}
func handleConnClients(c *websocket.Conn) {
roomId, roomIdInt := getRoomId()
objColl := collection.NewObjCollection(rooms[roomIdInt])
retColl := safe.Safety.Do(func() interface{} {
return objColl.Reject(func(item interface{}, key int) bool {
if item.(wsClients).Uid == clientMsg.Data.Uid {
chNotify <- 1
item.(wsClients).Conn.WriteMessage(websocket.TextMessage, []byte(`{"status":-1,"data":[]}`))
<-chNotify
return true
}
return false
})
}).(collection.ICollection)
retColl = safe.Safety.Do(func() interface{} {
return retColl.Append(wsClients{
Conn: c,
RemoteAddr: c.RemoteAddr().String(),
Uid: clientMsg.Data.Uid,
Username: clientMsg.Data.Username,
RoomId: roomId,
AvatarId: clientMsg.Data.AvatarId,
})
}).(collection.ICollection)
interfaces, _ := retColl.ToInterfaces()
rooms[roomIdInt] = interfaces
}
// 获取私聊的用户连接
func findToUserCoonClient() interface{} {
_, roomIdInt := getRoomId()
toUserUid := clientMsg.Data.ToUid
assignRoom := rooms[roomIdInt]
for _, c := range assignRoom {
stringUid := c.(wsClients).Uid
if stringUid == toUserUid {
return c
}
}
return nil
}
// 统一消息发放
func notify(conn *websocket.Conn, msg string) {
chNotify <- 1 // 利用channel阻塞 避免并发去对同一个连接发送消息出现panic: concurrent write to websocket connection这样的异常
_, roomIdInt := getRoomId()
assignRoom := rooms[roomIdInt]
for _, con := range assignRoom {
if con.(wsClients).RemoteAddr != conn.RemoteAddr().String() {
con.(wsClients).Conn.WriteMessage(websocket.TextMessage, []byte(msg))
}
}
<-chNotify
}
// 离线通知
func disconnect(conn *websocket.Conn) {
_, roomIdInt := getRoomId()
objColl := collection.NewObjCollection(rooms[roomIdInt])
retColl := safe.Safety.Do(func() interface{} {
return objColl.Reject(func(item interface{}, key int) bool {
if item.(wsClients).RemoteAddr == conn.RemoteAddr().String() {
data := msgData{
Username: item.(wsClients).Username,
Uid: item.(wsClients).Uid,
Time: time.Now().UnixNano() / 1e6, // 13位 10位 => now.Unix()
}
jsonStrServeMsg := msg{
Status: msgTypeOffline,
Data: data,
}
serveMsgStr, _ := json.Marshal(jsonStrServeMsg)
disMsg := string(serveMsgStr)
item.(wsClients).Conn.Close()
notify(conn, disMsg)
return true
}
return false
})
}).(collection.ICollection)
interfaces, _ := retColl.ToInterfaces()
rooms[roomIdInt] = interfaces
}
// 格式化传送给客户端的消息数据
func formatServeMsgStr(status int, conn *websocket.Conn) ([]byte, msg) {
roomId, roomIdInt := getRoomId()
//log.Println(reflect.TypeOf(var))
data := msgData{
Username: clientMsg.Data.Username,
Uid: clientMsg.Data.Uid,
RoomId: roomId,
Time: time.Now().UnixNano() / 1e6, // 13位 10位 => now.Unix()
}
if status == msgTypeSend || status == msgTypePrivateChat {
data.AvatarId = clientMsg.Data.AvatarId
content := clientMsg.Data.Content
data.Content = content
if helper.MbStrLen(content) > 800 {
// 直接截断
data.Content = string([]rune(content)[:800])
}
toUidStr := clientMsg.Data.ToUid
toUid, _ := strconv.Atoi(toUidStr)
// 保存消息
stringUid := data.Uid
intUid, _ := strconv.Atoi(stringUid)
if clientMsg.Data.ImageUrl != "" {
// 存在图片
models.SaveContent(map[string]interface{}{
"user_id": intUid,
"to_user_id": toUid,
"content": data.Content,
"room_id": data.RoomId,
"image_url": clientMsg.Data.ImageUrl,
})
} else {
models.SaveContent(map[string]interface{}{
"user_id": intUid,
"to_user_id": toUid,
"content": data.Content,
"room_id": data.RoomId,
})
}
}
if status == msgTypeGetOnlineUser {
ro := rooms[roomIdInt]
data.Count = len(ro)
data.List = ro
}
jsonStrServeMsg := msg{
Status: status,
Data: data,
Conn: conn,
}
serveMsgStr, _ := json.Marshal(jsonStrServeMsg)
return serveMsgStr, jsonStrServeMsg
}
func getRoomId() (string, int) {
roomId := clientMsg.Data.RoomId
roomIdInt, _ := strconv.Atoi(roomId)
return roomId, roomIdInt
}
// =======================对外方法=====================================
func GetOnlineUserCount() int {
num := 0
for i := 1; i <= roomCount; i++ {
num = num + GetOnlineRoomUserCount(i)
}
return num
}
func GetOnlineRoomUserCount(roomId int) int {
return len(rooms[roomId])
}