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