@@ -23,7 +23,7 @@ const (
23
23
AUTH_FAIL = "auth_fail"
24
24
CLOSE_SESSION = "close_session"
25
25
CONTENT_CHANGE = "content_change"
26
- PING = "ping"
26
+ PING = "ping"
27
27
)
28
28
29
29
var upgrader = websocket.Upgrader {
@@ -140,7 +140,9 @@ func (h *Hub) Run() {
140
140
case message := <- h .broadcast :
141
141
h .mutex .Lock ()
142
142
// Update the current workspace for this RoomID
143
- h .workspaces [message .RoomID ] = message .Content
143
+ if message .Content != "" {
144
+ h .workspaces [message .RoomID ] = message .Content
145
+ }
144
146
for client := range h .clients {
145
147
if client .roomID == message .RoomID {
146
148
@@ -163,8 +165,6 @@ func (h *Hub) Run() {
163
165
h .mutex .Unlock ()
164
166
}
165
167
166
-
167
-
168
168
}
169
169
}
170
170
@@ -306,14 +306,14 @@ func handleMessages(
306
306
// if msgData["type"] == "ping" {
307
307
// //receives ping from client1, need to send a ping to client2
308
308
// //eventually, if present, client2 will send the ping back, which will be broadcasted back to client1.
309
-
309
+
310
310
// userID, _ := msgData["userId"].(string)
311
311
// request := Message {
312
312
// RoomID: client.roomID,
313
313
// UserID: userID,
314
314
// Content: []byte("ping request"),
315
315
// }
316
-
316
+
317
317
// hub.broadcast <- request
318
318
// }
319
319
@@ -324,17 +324,30 @@ func handleMessages(
324
324
Type : msgData .Type ,
325
325
}
326
326
targetId := msgData .UserID
327
- data , err := persistMappings .Conn .HGetAll (context .Background (), targetId ).Result ()
327
+ ownData , err := persistMappings .Conn .HGetAll (context .Background (), targetId ).Result ()
328
328
if err != nil {
329
329
log .Printf ("Error retrieving data for userID %s: %v" , targetId , err )
330
330
} else {
331
- _ , err1 := persistMappings .Conn .Del (context .Background (), targetId ).Result ()
332
- if err1 != nil {
333
- log .Printf ("Error deleting data for userID %s: %v" , targetId , err1 )
331
+ // delete room under user id if it curr matches the room ID
332
+ ownRoomId := ownData ["roomId" ]
333
+ if ownRoomId == client .roomID {
334
+ _ , err := persistMappings .Conn .Del (context .Background (), targetId ).Result ()
335
+ if err != nil {
336
+ log .Printf ("Error deleting data for userID %s: %v" , targetId , err )
337
+ }
334
338
}
335
- _ , err2 := persistMappings .Conn .Del (context .Background (), data ["otherUser" ]).Result ()
336
- if err2 != nil {
337
- log .Printf ("Error deleting data for other user %s: %v" , data ["otherUser" ], err2 )
339
+ // delete room under other user if it curr matches the room ID
340
+ otherUser := ownData ["otherUser" ]
341
+ othRoomId , err := persistMappings .Conn .HGet (context .Background (), otherUser , "roomId" ).Result ()
342
+ if err != nil {
343
+ log .Printf ("Error retrieving data for otherUser %s: %v" , otherUser , err )
344
+ } else {
345
+ if othRoomId == client .roomID {
346
+ _ , err := persistMappings .Conn .Del (context .Background (), otherUser ).Result ()
347
+ if err != nil {
348
+ log .Printf ("Error deleting data for other user %s: %v" , otherUser , err )
349
+ }
350
+ }
338
351
}
339
352
}
340
353
hub .broadcast <- closeMessage
@@ -349,29 +362,28 @@ func handleMessages(
349
362
} else if msgData .Type == PING {
350
363
// Broadcast the message to other clients
351
364
hub .broadcast <- Message {
352
- RoomID : client .roomID ,
353
- Type : msgData .Type ,
354
- UserID : msgData .UserID ,
365
+ RoomID : client .roomID ,
366
+ Type : msgData .Type ,
367
+ UserID : msgData .UserID ,
355
368
}
356
369
357
370
extendExpiryTime (msgData .UserID , persistMappings )
358
371
} else {
359
372
log .Printf ("Unknown message type: %s" , msgData .Type )
360
- }
373
+ }
361
374
}
362
375
}
363
376
364
377
func extendExpiryTime (userId string , persistMappings * verify.PersistMappings ) {
365
-
378
+
366
379
ctx := context .Background ()
367
- if err := persistMappings .Conn .Expire (ctx , userId , time .Minute * 10 ).Err (); err != nil {
380
+ if err := persistMappings .Conn .Expire (ctx , userId , time .Minute * 10 ).Err (); err != nil {
368
381
log .Println ("Error extending room time on ping: " , err .Error ())
369
382
} else {
370
-
383
+
371
384
log .Printf ("expiration reset for 10 minutes for user %s: " , userId )
372
385
}
373
-
374
-
386
+
375
387
}
376
388
377
389
type ClientWorkspace struct {
0 commit comments