feat: update client management
- change Clients map to use net.Addr as the key type
- update `HandleConnection()` to store clients using LocalAddr
- modify `handleClientCommands()` to delete clients by LocalAddr
🤖
This commit is contained in:
parent
ebb58a4489
commit
89751ee7ea
2 changed files with 5 additions and 4 deletions
|
@ -18,7 +18,7 @@ func handleClientCommands(c models.WebsocketClient) {
|
||||||
_, message, err := ws.ReadMessage()
|
_, message, err := ws.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("Client disconnected:", "msg", err, "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)-1)
|
log.Info("Client disconnected:", "msg", err, "host", ws.NetConn().RemoteAddr(), "clients", len(Clients)-1)
|
||||||
delete(Clients, ws)
|
delete(Clients, ws.LocalAddr())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,14 +3,15 @@ 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 Client struct
|
// Clients is a map of connected WebSocket clients, where each client is represented by the WebsocketClient struct
|
||||||
var Clients = make(map[*websocket.Conn]*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
|
||||||
|
|
||||||
// Upgrade HTTP requests to WebSocket connections
|
// Upgrade HTTP requests to WebSocket connections
|
||||||
|
@ -35,7 +36,7 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) {
|
||||||
Conn: ws,
|
Conn: ws,
|
||||||
}
|
}
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
Clients[ws] = &client
|
Clients[ws.LocalAddr()] = &client
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
// Listen for commands from the connected client
|
// Listen for commands from the connected client
|
||||||
|
|
Loading…
Reference in a new issue