feat: rename Client
model to WebsocketClient
🤖
This commit is contained in:
parent
2d2ea6ff78
commit
ebb58a4489
3 changed files with 5 additions and 5 deletions
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Listens for commands from a client and handles them
|
// Listens for commands from a client and handles them
|
||||||
func handleClientCommands(c models.Client) {
|
func handleClientCommands(c models.WebsocketClient) {
|
||||||
ws := c.Conn
|
ws := c.Conn
|
||||||
for {
|
for {
|
||||||
var clientCommand models.ClientCommand
|
var clientCommand models.ClientCommand
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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 Client struct
|
||||||
var Clients = make(map[*websocket.Conn]*models.Client)
|
var Clients = make(map[*websocket.Conn]*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
|
||||||
|
@ -31,7 +31,7 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Info("Client connected", "host", ws.NetConn().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.Client{
|
client := models.WebsocketClient{
|
||||||
Conn: ws,
|
Conn: ws,
|
||||||
}
|
}
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
|
|
|
@ -13,13 +13,13 @@ type ClientCommand struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Represents a single client
|
// Represents a single client
|
||||||
type Client struct {
|
type WebsocketClient 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 *Client) SendMessage(messageType int, data []byte) error {
|
func (c *WebsocketClient) 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)
|
||||||
|
|
Loading…
Reference in a new issue