Compare commits

...

2 commits

Author SHA1 Message Date
478ff99fdd add newWebsocketClient() 2024-11-20 21:22:46 +01:00
191a90cf8c add client.IsStale() 2024-11-20 21:22:12 +01:00
3 changed files with 37 additions and 13 deletions

View file

@ -22,11 +22,12 @@ func SendPermanentBroadCastMessage() {
// Iterate over all connected clients and broadcast the message // Iterate over all connected clients and broadcast the message
for _, client := range Clients { for _, client := range Clients {
if time.Now().Sub(client.LastPong) > (10 * time.Second) {
// Remove unresponsive client // Remove unresponsive client
if client.IsStale() {
client.Conn.Close() client.Conn.Close()
continue continue
} }
// Send message to client
err := client.SendMessage(websocket.TextMessage, jsonMessage) err := client.SendMessage(websocket.TextMessage, jsonMessage)
if err != nil { if err != nil {
helper.Logger.Info("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients)) helper.Logger.Info("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients))

View file

@ -20,6 +20,22 @@ var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true }, CheckOrigin: func(r *http.Request) bool { return true },
} }
// initialize a new WebsocketClient
func newWebsocketClient(ws *websocket.Conn, address string) models.WebsocketClient {
wsclient := models.WebsocketClient{
Conn: ws,
LastPong: time.Now(),
RealIP: address,
}
wsclient.Conn.SetPongHandler(func(appData string) error {
wsclient.LastPong = time.Now()
return nil
})
return wsclient
}
// Upgrades HTTP requests to WebSocket connections and manages the client lifecycle // Upgrades HTTP requests to WebSocket connections and manages the client lifecycle
func HandleConnection(w http.ResponseWriter, r *http.Request) { func HandleConnection(w http.ResponseWriter, r *http.Request) {
// Upgrade initial GET request to a WebSocket // Upgrade initial GET request to a WebSocket
@ -31,11 +47,8 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) {
defer ws.Close() defer ws.Close()
// Register the new client // Register the new client
client := models.WebsocketClient{ client := newWebsocketClient(ws, r.RemoteAddr)
Conn: ws,
LastPong: time.Now(),
RealIP: r.RemoteAddr,
}
mu.Lock() mu.Lock()
Clients[ws.LocalAddr()] = &client Clients[ws.LocalAddr()] = &client
mu.Unlock() mu.Unlock()

View file

@ -24,12 +24,22 @@ type WebsocketClient struct {
// Sends a message to the websocket. // Sends a message to the websocket.
func (c *WebsocketClient) SendMessage(messageType int, data []byte) error { func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
c.Conn.SetPongHandler(func(s string) error { c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
c.LastPong = time.Now() err := c.Conn.WriteMessage(websocket.PingMessage, nil)
return nil if err != nil {
}) return err
}
c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second)) c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
c.Conn.WriteMessage(websocket.PingMessage, nil) err = c.Conn.WriteMessage(messageType, data)
return c.Conn.WriteMessage(messageType, data) if err != nil {
return err
}
return nil
}
// Check if websockets last Pong is recent
func (c *WebsocketClient) IsStale() bool {
return time.Since(c.LastPong) > (TIMEOUT * time.Second)
} }