another try to remove vanished clients
This commit is contained in:
parent
75b3ae87d6
commit
03d94d5999
2 changed files with 6 additions and 9 deletions
|
@ -9,8 +9,6 @@ import (
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
const TIMEOUT = 10
|
|
||||||
|
|
||||||
// Sends continous messages to all connected WebSocket clients
|
// Sends continous messages to all connected WebSocket clients
|
||||||
func SendPermanentBroadCastMessage() {
|
func SendPermanentBroadCastMessage() {
|
||||||
tick := time.NewTicker(time.Second)
|
tick := time.NewTicker(time.Second)
|
||||||
|
@ -24,10 +22,10 @@ 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 {
|
||||||
client.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
|
|
||||||
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Remove client on error
|
// Remove client on error
|
||||||
|
client.Conn.Close()
|
||||||
delete(Clients, client.Conn.LocalAddr())
|
delete(Clients, client.Conn.LocalAddr())
|
||||||
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))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/websocket"
|
"time"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const TIMEOUT = 10
|
||||||
|
|
||||||
// Represents a command from the client (start/stop)
|
// Represents a command from the client (start/stop)
|
||||||
type ClientCommand struct {
|
type ClientCommand struct {
|
||||||
Command string `json:"command"` // Command send to the server
|
Command string `json:"command"` // Command send to the server
|
||||||
|
@ -22,10 +24,7 @@ type WebsocketClient struct {
|
||||||
// Sends a message to the websocket.
|
// Sends a message to the websocket.
|
||||||
// Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time.
|
// Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time.
|
||||||
func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
|
func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
|
||||||
|
c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
|
||||||
err := c.Conn.WriteMessage(messageType, data)
|
err := c.Conn.WriteMessage(messageType, data)
|
||||||
if err != nil {
|
|
||||||
helper.Logger.Error("Error writing to WebSocket:", "msg", err)
|
|
||||||
c.Conn.Close() // Close the connection on error
|
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue