refactor: drop SendMessage()
method
This commit is contained in:
parent
ed67e9bc69
commit
1e9b7fbb79
4 changed files with 5 additions and 11 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 },
|
||||||
|
|
|
@ -8,13 +8,12 @@ import (
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
const TIMEOUT = 10
|
|
||||||
|
|
||||||
// Checks if the websockets last Pong is recent
|
// Checks if the websockets last Pong is recent
|
||||||
func isStale(client *models.WebsocketClient) bool {
|
func isStale(client *models.WebsocketClient) bool {
|
||||||
return time.Since(client.LastPong) > (TIMEOUT * time.Second)
|
return time.Since(client.LastPong) > (TIMEOUT * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// send a Ping to the websocket client
|
||||||
func sendPing(client *models.WebsocketClient) bool {
|
func sendPing(client *models.WebsocketClient) bool {
|
||||||
client.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
|
client.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
|
||||||
err := client.Conn.WriteMessage(websocket.PingMessage, nil)
|
err := client.Conn.WriteMessage(websocket.PingMessage, nil)
|
||||||
|
|
|
@ -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,9 +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))
|
|
||||||
return c.Conn.WriteMessage(messageType, data)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue