6
6
"fmt"
7
7
"log"
8
8
"net/http"
9
+ "strconv"
9
10
"strings"
10
11
"sync"
11
12
"time"
@@ -51,9 +52,18 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
51
52
52
53
// Connection successful. WebSocket is open.
53
54
55
+ // keepalive timeout
56
+ keepalive_seconds := KEEPALIVE_TIMEOUT_SECONDS
57
+ if keepalive_seconds_string := r .URL .Query ().Get ("keepalive_timeout_seconds" ); keepalive_seconds_string != "" {
58
+ if val , err := strconv .Atoi (keepalive_seconds_string ); err == nil && val >= 10 && val <= 600 {
59
+ keepalive_seconds = val
60
+ }
61
+ }
62
+ keepalive_duration := time .Duration (keepalive_seconds ) * time .Second
63
+
54
64
// Get connected at time and set automatic read timeout
55
65
connectedAtTimestamp := time .Now ().UTC ().Format (time .RFC3339Nano )
56
- conn .SetReadDeadline (time .Now ().Add (time . Second * KEEPALIVE_TIMEOUT_SECONDS ))
66
+ conn .SetReadDeadline (time .Now ().Add (keepalive_duration ))
57
67
58
68
client := & Client {
59
69
clientName : util .RandomGUID ()[:8 ],
@@ -129,7 +139,7 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
129
139
Session : WelcomeMessagePayloadSession {
130
140
ID : fmt .Sprintf ("%v_%v" , ws .ServerId , client .clientName ),
131
141
Status : "connected" ,
132
- KeepaliveTimeoutSeconds : KEEPALIVE_TIMEOUT_SECONDS ,
142
+ KeepaliveTimeoutSeconds : keepalive_seconds ,
133
143
ReconnectUrl : nil ,
134
144
ConnectedAt : connectedAtTimestamp ,
135
145
},
@@ -156,7 +166,7 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
156
166
}
157
167
158
168
// Set up ping/pong and keepalive handling
159
- client .keepAliveTimer = time .NewTicker (10 * time . Second )
169
+ client .keepAliveTimer = time .NewTicker (keepalive_duration )
160
170
client .pingTimer = time .NewTicker (5 * time .Second )
161
171
client .keepAliveLoopChan = make (chan struct {})
162
172
client .pingLoopChan = make (chan struct {})
0 commit comments