Compare commits
4 commits
2faa95c162
...
2d5fa692d3
Author | SHA1 | Date | |
---|---|---|---|
2d5fa692d3 | |||
2e7a4627e7 | |||
4fc03c4180 | |||
df98628d91 |
4 changed files with 29 additions and 27 deletions
|
@ -12,6 +12,7 @@ import (
|
||||||
// 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)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// Marshal the message into JSON format
|
// Marshal the message into JSON format
|
||||||
jsonMessage, err := json.Marshal(shared.State)
|
jsonMessage, err := json.Marshal(shared.State)
|
||||||
|
@ -24,7 +25,9 @@ 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(SEND_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))
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,17 +2,35 @@ package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func sendPing(client *models.WebsocketClient) bool {
|
||||||
|
client.Conn.SetWriteDeadline(time.Now().Add(SEND_TIMEOUT * time.Second))
|
||||||
|
err := client.Conn.WriteMessage(websocket.PingMessage, nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func isStale(client *models.WebsocketClient) bool {
|
||||||
|
return time.Since(client.LastPong) > (STALE_CLIENT_TIMEOUT * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
// Check and remove stale clients
|
// Check and remove stale clients
|
||||||
func RemoveStaleClients() {
|
func RemoveStaleClients() {
|
||||||
ticker := time.NewTicker(30 * time.Second)
|
ticker := time.NewTicker(CHECK_INTERVALL * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
for _, client := range Clients {
|
for _, client := range Clients {
|
||||||
if client.IsStale() {
|
if !sendPing(client) || isStale(client) {
|
||||||
client.Conn.Close()
|
client.Conn.Close()
|
||||||
delete(Clients, client.Conn.LocalAddr())
|
delete(Clients, client.Conn.LocalAddr())
|
||||||
}
|
}
|
||||||
|
|
5
internal/websocket/timeouts.go
Normal file
5
internal/websocket/timeouts.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
const SEND_TIMEOUT = 10
|
||||||
|
const STALE_CLIENT_TIMEOUT = 90
|
||||||
|
const CHECK_INTERVALL = 30
|
|
@ -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