Skip to content

Commit

Permalink
Added additional goroutines for client's loop channels, and set chann…
Browse files Browse the repository at this point in the history
…els to nil when closed
  • Loading branch information
Xemdo committed Apr 20, 2023
1 parent 128f0ca commit 7936cba
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions internal/events/websocket/mock_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,21 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
client.pingLoopChan = make(chan struct{})
client.keepAliveChanOpen = true
client.pingChanOpen = true
go func() {
// Set pong handler. Resets the read deadline when pong is received.
conn.SetPongHandler(func(string) error {
conn.SetReadDeadline(time.Now().Add(time.Second * KEEPALIVE_TIMEOUT_SECONDS))
return nil
})

// Set pong handler. Resets the read deadline when pong is received.
conn.SetPongHandler(func(string) error {
conn.SetReadDeadline(time.Now().Add(time.Second * KEEPALIVE_TIMEOUT_SECONDS))
return nil
})

// Keep Alive message loop
go func() {
for {
select {
case <-client.keepAliveLoopChan:
client.keepAliveTimer.Stop()
client.keepAliveLoopChan = nil
break

case <-client.keepAliveTimer.C: // Send KeepAlive message
keepAliveMsg, _ := json.Marshal(
Expand All @@ -192,9 +196,18 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
if ws.DebugEnabled {
log.Printf("Sent session_keepalive to client [%s]", client.clientName)
}
}
}
}()

// Ping/pong handler loop
go func() {
for {
select {
case <-client.pingLoopChan:
client.pingTimer.Stop()
client.pingLoopChan = nil
break

case <-client.pingTimer.C: // Send ping
err := client.SendMessage(websocket.PingMessage, []byte{})
Expand Down

0 comments on commit 7936cba

Please sign in to comment.