Compare commits
2 commits
2faa95c162
...
1e9b7fbb79
Author | SHA1 | Date | |
---|---|---|---|
1e9b7fbb79 | |||
ed67e9bc69 |
4 changed files with 25 additions and 26 deletions
|
@ -24,7 +24,8 @@ func SendPermanentBroadCastMessage() {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
for _, client := range Clients {
|
for _, client := range Clients {
|
||||||
// Send message to client
|
// Send message to client
|
||||||
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
client.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
|
||||||
|
err := client.Conn.WriteMessage(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))
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,8 @@ import (
|
||||||
var Clients = make(map[net.Addr]*models.WebsocketClient)
|
var Clients = make(map[net.Addr]*models.WebsocketClient)
|
||||||
var mu sync.Mutex // Mutex to protect access to the Clients map
|
var mu sync.Mutex // Mutex to protect access to the Clients map
|
||||||
|
|
||||||
|
const TIMEOUT = 10
|
||||||
|
|
||||||
// Upgrade HTTP requests to WebSocket connections
|
// Upgrade HTTP requests to WebSocket connections
|
||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
|
|
|
@ -2,8 +2,27 @@ package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Checks if the websockets last Pong is recent
|
||||||
|
func isStale(client *models.WebsocketClient) bool {
|
||||||
|
return time.Since(client.LastPong) > (TIMEOUT * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
// send a Ping to the websocket client
|
||||||
|
func sendPing(client *models.WebsocketClient) bool {
|
||||||
|
client.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
|
||||||
|
err := client.Conn.WriteMessage(websocket.PingMessage, nil)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// Check and remove stale clients
|
// Check and remove stale clients
|
||||||
func RemoveStaleClients() {
|
func RemoveStaleClients() {
|
||||||
ticker := time.NewTicker(30 * time.Second)
|
ticker := time.NewTicker(30 * time.Second)
|
||||||
|
@ -12,7 +31,8 @@ func RemoveStaleClients() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
for _, client := range Clients {
|
for _, client := range Clients {
|
||||||
if client.IsStale() {
|
// send ping and check for last pong
|
||||||
|
if !sendPing(client) || isStale(client) {
|
||||||
client.Conn.Close()
|
client.Conn.Close()
|
||||||
delete(Clients, client.Conn.LocalAddr())
|
delete(Clients, client.Conn.LocalAddr())
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,6 @@ import (
|
||||||
"github.com/gorilla/websocket"
|
"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
|
||||||
|
@ -21,25 +19,3 @@ type WebsocketClient struct {
|
||||||
LastPong time.Time
|
LastPong time.Time
|
||||||
RealIP string
|
RealIP string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sends a message to the websocket.
|
|
||||||
func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
|
|
||||||
c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
|
|
||||||
err := c.Conn.WriteMessage(websocket.PingMessage, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
|
|
||||||
err = c.Conn.WriteMessage(messageType, data)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Checks if the websockets last Pong is recent
|
|
||||||
func (c *WebsocketClient) IsStale() bool {
|
|
||||||
return time.Since(c.LastPong) > (TIMEOUT * time.Second)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue