7
7
"log"
8
8
"net/http"
9
9
"os"
10
- "sync"
11
10
"strconv"
11
+ "sync"
12
12
13
13
"github.com/gin-gonic/gin"
14
14
"github.com/gorilla/websocket"
@@ -21,23 +21,25 @@ var upgrader = websocket.Upgrader{
21
21
}
22
22
23
23
type Client struct {
24
- conn * websocket.Conn
25
- roomID string
24
+ conn * websocket.Conn
25
+ roomID string
26
26
authenticated bool
27
27
}
28
28
29
29
type Hub struct {
30
30
clients map [* Client ]bool
31
- workspaces map [string ]string
31
+ workspaces map [string ]string
32
32
broadcast chan Message
33
33
register chan * Client
34
34
unregister chan * Client
35
35
mutex sync.Mutex
36
36
}
37
37
38
38
type Message struct {
39
- roomID string
40
- content []byte
39
+ Type string `json:"type"`
40
+ RoomID string `json:"roomId"`
41
+ Content []byte `json:"data"`
42
+ UserID string `json:"userId"`
41
43
}
42
44
43
45
func verifyToken (token string ) (bool , string ) {
@@ -71,7 +73,7 @@ func verifyToken(token string) (bool, string) {
71
73
} `json:"data"`
72
74
}
73
75
74
- body , err := io .ReadAll (resp .Body )
76
+ body , err := io .ReadAll (resp .Body )
75
77
if err != nil {
76
78
log .Println ("Error reading response body:" , err )
77
79
return false , ""
@@ -97,7 +99,7 @@ func verifyToken(token string) (bool, string) {
97
99
func NewHub () * Hub {
98
100
return & Hub {
99
101
clients : make (map [* Client ]bool ),
100
- workspaces : make (map [string ]string ),
102
+ workspaces : make (map [string ]string ),
101
103
broadcast : make (chan Message ),
102
104
register : make (chan * Client ),
103
105
unregister : make (chan * Client ),
@@ -123,11 +125,11 @@ func (h *Hub) Run() {
123
125
124
126
case message := <- h .broadcast :
125
127
h .mutex .Lock ()
126
- // Update the current workspace for this roomID
127
- h .workspaces [message .roomID ] = string (message .content )
128
+ // Update the current workspace for this RoomID
129
+ h .workspaces [message .RoomID ] = string (message .Content )
128
130
for client := range h .clients {
129
- if client .roomID == message .roomID {
130
- err := client .conn .WriteMessage (websocket .TextMessage , message .content )
131
+ if client .roomID == message .RoomID {
132
+ err := client .conn .WriteMessage (websocket .TextMessage , message .Content )
131
133
if err != nil {
132
134
log .Printf ("Error sending message: %v" , err )
133
135
client .conn .Close ()
@@ -142,9 +144,9 @@ func (h *Hub) Run() {
142
144
143
145
// ServeWs handles WebSocket requests
144
146
func serveWs (
145
- hub * Hub , c * gin.Context ,
146
- roomMappings * verify.RoomMappings ,
147
- persistMappings * verify.PersistMappings ,
147
+ hub * Hub , c * gin.Context ,
148
+ roomMappings * verify.RoomMappings ,
149
+ persistMappings * verify.PersistMappings ,
148
150
) {
149
151
log .Println ("handler called!" )
150
152
roomID := c .Query ("roomID" )
@@ -166,9 +168,9 @@ func serveWs(
166
168
}
167
169
168
170
func authenticateClient (
169
- token string , match string , client * Client ,
170
- roomMappings * verify.RoomMappings ,
171
- persistMappings * verify.PersistMappings ,
171
+ token string , match string , client * Client ,
172
+ roomMappings * verify.RoomMappings ,
173
+ persistMappings * verify.PersistMappings ,
172
174
) bool {
173
175
ok , userID := verifyToken (token )
174
176
if ! ok {
@@ -180,8 +182,8 @@ func authenticateClient(
180
182
}
181
183
182
184
func authenticateClientNoMatch (
183
- token string , client * Client ,
184
- persistMappings * verify.PersistMappings ,
185
+ token string , client * Client ,
186
+ persistMappings * verify.PersistMappings ,
185
187
) bool {
186
188
ok , userID := verifyToken (token )
187
189
if ! ok {
@@ -192,9 +194,9 @@ func authenticateClientNoMatch(
192
194
}
193
195
194
196
func handleMessages (
195
- client * Client , hub * Hub ,
196
- roomMappings * verify.RoomMappings ,
197
- persistMappings * verify.PersistMappings ,
197
+ client * Client , hub * Hub ,
198
+ roomMappings * verify.RoomMappings ,
199
+ persistMappings * verify.PersistMappings ,
198
200
) {
199
201
defer func () {
200
202
hub .unregister <- client
@@ -212,18 +214,17 @@ func handleMessages(
212
214
log .Printf ("Failed to parse message: %v" , err )
213
215
continue
214
216
}
215
- // Handle authentication message
216
217
if msgData ["type" ] == "auth" {
217
- token , tokenOk := msgData ["token" ].(string )
218
- if ! tokenOk {
219
- log .Println ("Authentication failed - no token attached" )
220
- client .conn .WriteMessage (
221
- websocket .TextMessage ,
222
- []byte ("Authentication failed - no JWT token" ),
223
- )
224
- client .conn .Close ()
225
- break
226
- }
218
+ token , tokenOk := msgData ["token" ].(string )
219
+ if ! tokenOk {
220
+ log .Println ("Authentication failed - no token attached" )
221
+ client .conn .WriteMessage (
222
+ websocket .TextMessage ,
223
+ []byte ("Authentication failed - no JWT token" ),
224
+ )
225
+ client .conn .Close ()
226
+ break
227
+ }
227
228
isSuccess := false
228
229
match , matchOk := msgData ["matchHash" ].(string )
229
230
if matchOk && ! authenticateClient (token , match , client , roomMappings , persistMappings ) {
@@ -246,20 +247,21 @@ func handleMessages(
246
247
client .conn .Close ()
247
248
break
248
249
}
249
- client .authenticated = true
250
- log .Println ("Client authenticated successfully" )
251
- }
250
+ client .authenticated = true
251
+ log .Println ("Client authenticated successfully" )
252
+ }
252
253
253
254
if msgData ["type" ] == "close_session" {
254
255
closeMessage := Message {
255
- roomID : client .roomID ,
256
- content : []byte ("The session has been closed by a user." ),
256
+ RoomID : client .roomID ,
257
+ Content : []byte ("The session has been closed by a user." ),
257
258
}
258
259
hub .broadcast <- closeMessage
259
260
}
260
261
261
262
// Broadcast the message to other clients
262
- hub .broadcast <- Message {roomID : client .roomID , content : message }
263
+ userID , _ := msgData ["userId" ].(string )
264
+ hub .broadcast <- Message {RoomID : client .roomID , Content : message , UserID : userID }
263
265
}
264
266
}
265
267
@@ -273,17 +275,17 @@ func statusHandler(hub *Hub) gin.HandlerFunc {
273
275
for client := range hub .clients {
274
276
roomID := client .roomID
275
277
currentStatus , ok := status [roomID ]
276
- if ( ! ok ) {
278
+ if ! ok {
277
279
// Initialize status for a new roomID
278
280
status [roomID ] = map [string ]interface {}{
279
- "clients" : 1 ,
280
- "workspace" : hub .workspaces [roomID ],
281
+ "clients" : 1 ,
282
+ "workspace" : hub .workspaces [roomID ],
281
283
}
282
284
} else {
283
285
// Update the client count for an existing roomID
284
286
status [roomID ] = map [string ]interface {}{
285
- "clients" : currentStatus .(map [string ]interface {})["clients" ].(int ) + 1 ,
286
- "workspace" : hub .workspaces [roomID ],
287
+ "clients" : currentStatus .(map [string ]interface {})["clients" ].(int ) + 1 ,
288
+ "workspace" : hub .workspaces [roomID ],
287
289
}
288
290
}
289
291
}
@@ -292,7 +294,6 @@ func statusHandler(hub *Hub) gin.HandlerFunc {
292
294
}
293
295
}
294
296
295
-
296
297
func main () {
297
298
r := gin .Default ()
298
299
hub := NewHub ()
@@ -324,8 +325,8 @@ func main() {
324
325
}
325
326
}
326
327
327
- roomMappings := verify .InitialiseRoomMappings (REDIS_URI , REDIS_ROOM_MAPPING )
328
- persistMappings := verify .InitialisePersistMappings (REDIS_URI , REDIS_ROOM_PERSIST );
328
+ roomMappings := verify .InitialiseRoomMappings (REDIS_URI , REDIS_ROOM_MAPPING )
329
+ persistMappings := verify .InitialisePersistMappings (REDIS_URI , REDIS_ROOM_PERSIST )
329
330
330
331
// WebSocket connection endpoint
331
332
r .GET ("/ws" , func (c * gin.Context ) {
0 commit comments