This commit is contained in:
Fangliding
2026-11-23 02:29:56 +08:00
parent 3ff9cec713
commit 8d6509bdcd
6 changed files with 21 additions and 32 deletions
@@ -3,7 +3,6 @@ package websocket
import (
"io"
"net"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
@@ -33,14 +32,8 @@ func NewConnection(conn *websocket.Conn, remoteAddr net.Addr, extraReader io.Rea
reader: extraReader,
}
if heartbeatPeriod != 0 {
var checked atomic.Bool
keepalive := task.Periodic{
Execute: func() error {
// task.Periodic will try to execute this function immediately, drop it
if !checked.Load() {
checked.Store(true)
return nil
}
return conn.WriteControl(websocket.PingMessage, []byte{}, time.Time{})
},
Interval: time.Duration(heartbeatPeriod) * time.Second,