Skip to content

Commit

Permalink
fix: kill connection after broadcasthandler was stopped
Browse files Browse the repository at this point in the history
related to #28 and #29
  • Loading branch information
d-Rickyy-b committed Dec 16, 2023
1 parent 93e3a24 commit 8429ab5
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions internal/web/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ type client struct {

// Each client has a broadcastHandler that runs in the background and sends out the broadcast messages to the client.
func (c *client) broadcastHandler() {
defer func() {
log.Println("Closing broadcast handler for client:", c.conn.RemoteAddr())
_ = c.conn.SetWriteDeadline(time.Now().Add(5 * time.Second))
_ = c.conn.WriteMessage(websocket.CloseMessage, []byte{})
_ = c.conn.Close()
}()

for message := range c.broadcastChan {
c.conn.SetWriteDeadline(time.Now().Add(5 * time.Second)) //nolint:errcheck

Expand All @@ -40,8 +47,6 @@ func (c *client) broadcastHandler() {
return
}
}

_ = c.conn.WriteMessage(websocket.CloseMessage, []byte{})
}

// listenWebsocket is running in the background on a goroutine and listens for messages from the client.
Expand Down

0 comments on commit 8429ab5

Please sign in to comment.