From a81e8e2401e38e73c39ff573ddbf8be7e97b048c Mon Sep 17 00:00:00 2001 From: lesismal Date: Sat, 9 Dec 2023 23:57:44 +0800 Subject: [PATCH] websocket: set KeepaliveTime to 120s by default --- nbhttp/websocket/upgrader.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/nbhttp/websocket/upgrader.go b/nbhttp/websocket/upgrader.go index 1f073f90..32a83e58 100644 --- a/nbhttp/websocket/upgrader.go +++ b/nbhttp/websocket/upgrader.go @@ -131,8 +131,9 @@ func NewUpgrader() *Upgrader { u := &Upgrader{ commonFields: commonFields{ Engine: DefaultEngine, - compressionLevel: defaultCompressionLevel, + KeepaliveTime: nbhttp.DefaultKeepaliveTime, BlockingModAsyncCloseDelay: DefaultBlockingModAsyncCloseDelay, + compressionLevel: defaultCompressionLevel, }, BlockingModReadBufferSize: DefaultBlockingReadBufferSize, BlockingModAsyncWrite: DefaultBlockingModAsyncWrite, @@ -430,6 +431,12 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade return nil, err } + if u.KeepaliveTime > 0 { + conn.SetReadDeadline(time.Now().Add(u.KeepaliveTime)) + } else { + conn.SetReadDeadline(time.Time{}) + } + if wsc.openHandler != nil { wsc.openHandler(wsc) } @@ -560,12 +567,6 @@ func (u *Upgrader) commResponse(conn net.Conn, responseHeader http.Header, chall return err } - if u.KeepaliveTime <= 0 { - conn.SetReadDeadline(time.Now().Add(nbhttp.DefaultKeepaliveTime)) - } else { - conn.SetReadDeadline(time.Now().Add(u.KeepaliveTime)) - } - return nil }