Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57b214d

Browse files
committed
避免 goroutine leak
1 parent ea59abc commit 57b214d

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

http/controller/websocket.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func (this *WebsocketController) Ws(wsConn *websocket.Conn) {
5252

5353
messageChan := userData.MessageQueue(serverId)
5454

55+
ticker := time.NewTicker(15e9)
56+
defer ticker.Stop()
57+
5558
var clientClosed = false
5659
for {
5760
select {
@@ -61,7 +64,7 @@ func (this *WebsocketController) Ws(wsConn *websocket.Conn) {
6164
clientClosed = true
6265
}
6366
// 心跳
64-
case <-time.After(15e9):
67+
case <-ticker.C:
6568
if err := websocket.JSON.Send(wsConn, ""); err != nil {
6669
// logger.Errorln("Send heart message to user:", user, "server_id:", serverId, "error:", err)
6770
clientClosed = true

0 commit comments

Comments
 (0)