Skip to content

Commit 50c270c

Browse files
committed
use mutexes for websocket connections
1 parent 7a4c386 commit 50c270c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

http/gateway/handler.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gateway
33
import (
44
"fmt"
55
"math/rand"
6+
"sync"
67
"time"
78

89
"github.com/blobs-io/blobsgame/models/player"
@@ -14,8 +15,9 @@ import (
1415
)
1516

1617
type WebSocketConnection struct {
17-
Conn *websocket.Conn
18-
ID string
18+
Conn *websocket.Conn
19+
Mutex sync.Mutex
20+
ID string
1921
}
2022

2123
var connections = make(map[string]WebSocketConnection, 0)
@@ -168,10 +170,20 @@ func handleClose(c *WebSocketConnection) {
168170
}
169171

170172
func (c *WebSocketConnection) Send(d AnyMessage) error {
173+
c.Mutex.Lock()
174+
defer c.Mutex.Unlock()
171175
return c.Conn.WriteJSON(d)
172176
}
173177

174178
func (c *WebSocketConnection) Kick(r *room.Room, kickType uint8, reason string) error {
179+
fmt.Println(AnyMessage{
180+
Op: OpClose,
181+
T: PlayerKickEvent,
182+
Data: map[string]interface{}{
183+
"type": kickType,
184+
"message": reason,
185+
},
186+
})
175187
err := c.Send(AnyMessage{
176188
Op: OpClose,
177189
T: PlayerKickEvent,

0 commit comments

Comments
 (0)