添加聊天时心跳检测

This commit is contained in:
junleea 2024-07-03 17:37:22 +08:00
parent 22442eb828
commit 75a0cb9f3c
1 changed files with 18 additions and 7 deletions

View File

@ -178,19 +178,30 @@ func SRMessage(c *gin.Context) {
for {
if v := clients[ws]; v == true {
res2 := worker.PopRedisListLeft(res + "_" + strconv.Itoa(id1))
var res3 []byte
var msg proto.Message
if res2 != "" {
var msg proto.Message
msg.Type = "msg"
msg.Msg = res2
msg.From_user_id = id1
msg.Session = res
res3, _ := json.Marshal(msg)
err2 := ws.WriteMessage(websocket.TextMessage, res3)
if err2 != nil {
break
}
res3, _ = json.Marshal(msg)
} else {
//若无消息则发送心跳包
msg.Type = "check"
msg.Msg = "check"
msg.From_user_id = -1
msg.Session = res
res3, _ = json.Marshal(msg)
}
time.Sleep(time.Second * 1) // 每100毫秒查询一次
err2 := ws.WriteMessage(websocket.TextMessage, res3)
if err2 != nil {
clientsMux.Lock()
delete(clients, ws)
clientsMux.Unlock()
break
}
time.Sleep(time.Second * 1) // 每1秒查询一次
} else {
clientsMux.Lock()
delete(clients, ws)