Compare commits

..

No commits in common. "f8a047abdf583737a94cb0068bed15d932a9b0ee" and "2d2ea6ff7867319673895b35753d9bf226f1b382" have entirely different histories.

3 changed files with 10 additions and 11 deletions

View file

@ -10,15 +10,15 @@ import (
) )
// Listens for commands from a client and handles them // Listens for commands from a client and handles them
func handleClientCommands(c models.WebsocketClient) { func handleClientCommands(c models.Client) {
ws := c.Conn ws := c.Conn
for { for {
var clientCommand models.ClientCommand var clientCommand models.ClientCommand
_, message, err := ws.ReadMessage() _, message, err := ws.ReadMessage()
if err != nil { if err != nil {
log.Info("Client disconnected:", "msg", err, "host", ws.RemoteAddr(), "clients", len(Clients)-1) log.Info("Client disconnected:", "msg", err, "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)-1)
delete(Clients, ws.LocalAddr()) delete(Clients, ws)
break break
} }

View file

@ -3,15 +3,14 @@ package websocket
import ( import (
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"net"
"net/http" "net/http"
"sync" "sync"
"git.smsvc.net/pomodoro/GoTomato/pkg/models" "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
// Clients is a map of connected WebSocket clients, where each client is represented by the WebsocketClient struct // Clients is a map of connected WebSocket clients, where each client is represented by the Client struct
var Clients = make(map[net.Addr]*models.WebsocketClient) var Clients = make(map[*websocket.Conn]*models.Client)
var mu sync.Mutex // Mutex to protect access to the Clients map var mu sync.Mutex // Mutex to protect access to the Clients map
// Upgrade HTTP requests to WebSocket connections // Upgrade HTTP requests to WebSocket connections
@ -29,14 +28,14 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) {
} }
defer ws.Close() defer ws.Close()
log.Info("Client connected", "host", ws.RemoteAddr(), "clients", len(Clients)+1) log.Info("Client connected", "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)+1)
// Register the new client // Register the new client
client := models.WebsocketClient{ client := models.Client{
Conn: ws, Conn: ws,
} }
mu.Lock() mu.Lock()
Clients[ws.LocalAddr()] = &client Clients[ws] = &client
mu.Unlock() mu.Unlock()
// Listen for commands from the connected client // Listen for commands from the connected client

View file

@ -13,13 +13,13 @@ type ClientCommand struct {
} }
// Represents a single client // Represents a single client
type WebsocketClient struct { type Client struct {
Conn *websocket.Conn Conn *websocket.Conn
} }
// 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 *Client) SendMessage(messageType int, data []byte) error {
err := c.Conn.WriteMessage(messageType, data) err := c.Conn.WriteMessage(messageType, data)
if err != nil { if err != nil {
log.Error("Error writing to WebSocket:", "msg", err) log.Error("Error writing to WebSocket:", "msg", err)