不在应用层发送心跳

This commit is contained in:
junleea 2025-03-22 17:38:44 +08:00
parent 0ab5328bab
commit 6b235ba28c
1 changed files with 16 additions and 17 deletions

View File

@ -12,7 +12,6 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"sync" "sync"
"time"
) )
type SMessage struct { type SMessage struct {
@ -85,8 +84,8 @@ func subscribeAndHandleIMMessages(ws *websocket.Conn, userId int) {
defer ws.Close() defer ws.Close()
ch := pubsub.Channel() ch := pubsub.Channel()
//定时器每3秒发送一次心跳 //定时器每3秒发送一次心跳
ticker := time.NewTicker(3 * time.Second) //ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop() //defer ticker.Stop()
var sessionID uint var sessionID uint
@ -153,18 +152,18 @@ func subscribeAndHandleIMMessages(ws *websocket.Conn, userId int) {
}() }()
//发送心跳包 //发送心跳包
for { //for {
select { // select {
case <-done: // case <-done:
//客户端断开连接 // //客户端断开连接
return // return
case t := <-ticker.C: // case t := <-ticker.C:
//发送心跳 // //发送心跳
err := ws.WriteMessage(websocket.PingMessage, nil) // err := ws.WriteMessage(websocket.PingMessage, nil)
if err != nil { // if err != nil {
log.Println("Ping error:", err, t) // log.Println("Ping error:", err, t)
return // return
} // }
} // }
} //}
} }