forked from hezhizheng/go-gin-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.go
346 lines (276 loc) · 8.16 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
package go_ws
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"go-gin-chat/models"
"go-gin-chat/ws"
"log"
"net/http"
"strconv"
"sync"
"time"
)
// 客户端连接详情
type wsClients struct {
Conn *websocket.Conn `json:"conn"`
RemoteAddr string `json:"remote_addr"`
Uid float64 `json:"uid"`
Username string `json:"username"`
RoomId string `json:"room_id"`
AvatarId string `json:"avatar_id"`
}
// client & serve 的消息体
type msg struct {
Status int `json:"status"`
Data interface{} `json:"data"`
Conn *websocket.Conn `json:"conn"`
}
// 变量定义初始化
var (
wsUpgrader = websocket.Upgrader{}
clientMsg = msg{}
mutex = sync.Mutex{}
//rooms = [roomCount + 1][]wsClients{}
rooms = make(map[int][]wsClients)
enterRooms = make(chan wsClients)
sMsg = make(chan msg)
offline = make(chan *websocket.Conn)
chNotify = make(chan int ,1)
)
// 定义消息类型
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()
go read(c)
go write()
select {}
}
func read(c *websocket.Conn) {
defer func() {
//捕获read抛出的panic
if err := recover();err!=nil{
log.Println("read发生错误",err)
}
}()
for {
_, message, err := c.ReadMessage()
// log.Println("client message", string(message),c.RemoteAddr())
if err != nil { // 离线通知
offline <- c
log.Println("ReadMessage error1", err)
break
}
serveMsgStr := message
// 处理心跳响应 , heartbeat为与客户端约定的值
if string(serveMsgStr) == `heartbeat` {
c.WriteMessage(websocket.TextMessage, []byte(`{"status":0,"data":"heartbeat ok"}`))
continue
}
json.Unmarshal(message, &clientMsg)
// log.Println("来自客户端的消息", clientMsg,c.RemoteAddr())
if clientMsg.Data != nil {
if clientMsg.Status == msgTypeOnline { // 进入房间,建立连接
roomId, _ := getRoomId()
enterRooms <- wsClients{
Conn: c,
RemoteAddr: c.RemoteAddr().String(),
Uid: clientMsg.Data.(map[string]interface{})["uid"].(float64),
Username: clientMsg.Data.(map[string]interface{})["username"].(string),
RoomId: roomId,
AvatarId: clientMsg.Data.(map[string]interface{})["avatar_id"].(string),
}
}
_, serveMsg := formatServeMsgStr(clientMsg.Status, c)
sMsg <- serveMsg
}
}
}
func write() {
defer func() {
//捕获write抛出的panic
if err := recover();err!=nil{
log.Println("write发生错误",err)
}
}()
for {
select {
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()
for cKey, wcl := range rooms[roomIdInt] {
if wcl.Uid == clientMsg.Data.(map[string]interface{})["uid"].(float64) {
mutex.Lock()
// 通知当前用户下线
wcl.Conn.WriteMessage(websocket.TextMessage, []byte(`{"status":-1,"data":[]}`))
rooms[roomIdInt] = append(rooms[roomIdInt][:cKey], rooms[roomIdInt][cKey+1:]...)
wcl.Conn.Close()
mutex.Unlock()
}
}
mutex.Lock()
rooms[roomIdInt] = append(rooms[roomIdInt], wsClients{
Conn: c,
RemoteAddr: c.RemoteAddr().String(),
Uid: clientMsg.Data.(map[string]interface{})["uid"].(float64),
Username: clientMsg.Data.(map[string]interface{})["username"].(string),
RoomId: roomId,
AvatarId: clientMsg.Data.(map[string]interface{})["avatar_id"].(string),
})
mutex.Unlock()
}
// 获取私聊的用户连接
func findToUserCoonClient() interface{} {
_, roomIdInt := getRoomId()
toUserUid := clientMsg.Data.(map[string]interface{})["to_uid"].(string)
for _, c := range rooms[roomIdInt] {
stringUid := strconv.FormatFloat(c.Uid, 'f', -1, 64)
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()
for _, con := range rooms[roomIdInt] {
if con.RemoteAddr != conn.RemoteAddr().String() {
con.Conn.WriteMessage(websocket.TextMessage, []byte(msg))
}
}
<-chNotify
}
// 离线通知
func disconnect(conn *websocket.Conn) {
_, roomIdInt := getRoomId()
for index, con := range rooms[roomIdInt] {
if con.RemoteAddr == conn.RemoteAddr().String() {
data := map[string]interface{}{
"username": con.Username,
"uid": con.Uid,
"time": time.Now().UnixNano() / 1e6, // 13位 10位 => now.Unix()
}
jsonStrServeMsg := msg{
Status: msgTypeOffline,
Data: data,
}
serveMsgStr, _ := json.Marshal(jsonStrServeMsg)
disMsg := string(serveMsgStr)
mutex.Lock()
rooms[roomIdInt] = append(rooms[roomIdInt][:index], rooms[roomIdInt][index+1:]...)
con.Conn.Close()
mutex.Unlock()
notify(conn, disMsg)
}
}
}
// 格式化传送给客户端的消息数据
func formatServeMsgStr(status int, conn *websocket.Conn) ([]byte, msg) {
roomId, roomIdInt := getRoomId()
data := map[string]interface{}{
"username": clientMsg.Data.(map[string]interface{})["username"].(string),
"uid": clientMsg.Data.(map[string]interface{})["uid"].(float64),
"room_id": roomId,
"time": time.Now().UnixNano() / 1e6, // 13位 10位 => now.Unix()
}
if status == msgTypeSend || status == msgTypePrivateChat {
data["avatar_id"] = clientMsg.Data.(map[string]interface{})["avatar_id"].(string)
data["content"] = clientMsg.Data.(map[string]interface{})["content"].(string)
toUidStr := clientMsg.Data.(map[string]interface{})["to_uid"].(string)
toUid, _ := strconv.Atoi(toUidStr)
// 保存消息
stringUid := strconv.FormatFloat(data["uid"].(float64), 'f', -1, 64)
intUid, _ := strconv.Atoi(stringUid)
if _, ok := clientMsg.Data.(map[string]interface{})["image_url"]; ok {
// 存在图片
models.SaveContent(map[string]interface{}{
"user_id": intUid,
"to_user_id": toUid,
"content": data["content"],
"room_id": data["room_id"],
"image_url": clientMsg.Data.(map[string]interface{})["image_url"].(string),
})
} else {
models.SaveContent(map[string]interface{}{
"user_id": intUid,
"to_user_id": toUid,
"room_id": data["room_id"],
"content": data["content"],
})
}
}
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.(map[string]interface{})["room_id"].(string)
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])
}