Compare commits
2 commits
f8a047abdf
...
f4fd37c551
Author | SHA1 | Date | |
---|---|---|---|
f4fd37c551 | |||
0ee955189c |
2 changed files with 7 additions and 6 deletions
|
@ -17,8 +17,8 @@ func handleClientCommands(c models.WebsocketClient) {
|
|||
|
||||
_, message, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
log.Info("Client disconnected:", "msg", err, "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)-1)
|
||||
delete(Clients, ws)
|
||||
log.Info("Client disconnected:", "msg", err, "host", ws.RemoteAddr(), "clients", len(Clients)-1)
|
||||
delete(Clients, ws.LocalAddr())
|
||||
break
|
||||
}
|
||||
|
||||
|
|
|
@ -3,14 +3,15 @@ package websocket
|
|||
import (
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/gorilla/websocket"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
// Clients is a map of connected WebSocket clients, where each client is represented by the Client struct
|
||||
var Clients = make(map[*websocket.Conn]*models.WebsocketClient)
|
||||
// Clients is a map of connected WebSocket clients, where each client is represented by the WebsocketClient struct
|
||||
var Clients = make(map[net.Addr]*models.WebsocketClient)
|
||||
var mu sync.Mutex // Mutex to protect access to the Clients map
|
||||
|
||||
// Upgrade HTTP requests to WebSocket connections
|
||||
|
@ -28,14 +29,14 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
defer ws.Close()
|
||||
|
||||
log.Info("Client connected", "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)+1)
|
||||
log.Info("Client connected", "host", ws.RemoteAddr(), "clients", len(Clients)+1)
|
||||
|
||||
// Register the new client
|
||||
client := models.WebsocketClient{
|
||||
Conn: ws,
|
||||
}
|
||||
mu.Lock()
|
||||
Clients[ws] = &client
|
||||
Clients[ws.LocalAddr()] = &client
|
||||
mu.Unlock()
|
||||
|
||||
// Listen for commands from the connected client
|
||||
|
|
Loading…
Reference in a new issue