@@ -10,6 +10,7 @@ import (
10
10
"os"
11
11
"strconv"
12
12
"sync"
13
+ "time"
13
14
14
15
"github.com/gin-gonic/gin"
15
16
"github.com/gorilla/websocket"
@@ -22,7 +23,7 @@ const (
22
23
AUTH_FAIL = "auth_fail"
23
24
CLOSE_SESSION = "close_session"
24
25
CONTENT_CHANGE = "content_change"
25
- PING = "ping"
26
+ PING = "ping"
26
27
)
27
28
28
29
var upgrader = websocket.Upgrader {
@@ -139,7 +140,9 @@ func (h *Hub) Run() {
139
140
case message := <- h .broadcast :
140
141
h .mutex .Lock ()
141
142
// Update the current workspace for this RoomID
142
- h .workspaces [message .RoomID ] = message .Content
143
+ if message .Content != "" {
144
+ h .workspaces [message .RoomID ] = message .Content
145
+ }
143
146
for client := range h .clients {
144
147
if client .roomID == message .RoomID {
145
148
@@ -162,8 +165,6 @@ func (h *Hub) Run() {
162
165
h .mutex .Unlock ()
163
166
}
164
167
165
-
166
-
167
168
}
168
169
}
169
170
@@ -305,34 +306,48 @@ func handleMessages(
305
306
// if msgData["type"] == "ping" {
306
307
// //receives ping from client1, need to send a ping to client2
307
308
// //eventually, if present, client2 will send the ping back, which will be broadcasted back to client1.
308
-
309
+
309
310
// userID, _ := msgData["userId"].(string)
310
311
// request := Message {
311
312
// RoomID: client.roomID,
312
313
// UserID: userID,
313
314
// Content: []byte("ping request"),
314
315
// }
315
-
316
+
316
317
// hub.broadcast <- request
317
318
// }
318
319
319
320
if msgData .Type == CLOSE_SESSION {
320
321
closeMessage := Message {
322
+ Type : CLOSE_SESSION ,
321
323
RoomID : client .roomID ,
322
324
Content : "The session has been closed by a user." ,
323
325
}
324
326
targetId := msgData .UserID
325
- data , err := persistMappings .Conn .HGetAll (context .Background (), targetId ).Result ()
327
+ ownData , err := persistMappings .Conn .HGetAll (context .Background (), targetId ).Result ()
326
328
if err != nil {
327
329
log .Printf ("Error retrieving data for userID %s: %v" , targetId , err )
328
330
} else {
329
- _ , err1 := persistMappings .Conn .Del (context .Background (), targetId ).Result ()
330
- if err1 != nil {
331
- 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
+ }
332
338
}
333
- _ , err2 := persistMappings .Conn .Del (context .Background (), data ["otherUser" ]).Result ()
334
- if err2 != nil {
335
- 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
+ }
336
351
}
337
352
}
338
353
hub .broadcast <- closeMessage
@@ -347,16 +362,30 @@ func handleMessages(
347
362
} else if msgData .Type == PING {
348
363
// Broadcast the message to other clients
349
364
hub .broadcast <- Message {
350
- RoomID : client .roomID ,
351
- Type : msgData .Type ,
352
- UserID : msgData .UserID ,
365
+ RoomID : client .roomID ,
366
+ Type : msgData .Type ,
367
+ UserID : msgData .UserID ,
353
368
}
369
+
370
+ extendExpiryTime (msgData .UserID , persistMappings )
354
371
} else {
355
372
log .Printf ("Unknown message type: %s" , msgData .Type )
356
373
}
357
374
}
358
375
}
359
376
377
+ func extendExpiryTime (userId string , persistMappings * verify.PersistMappings ) {
378
+
379
+ ctx := context .Background ()
380
+ if err := persistMappings .Conn .Expire (ctx , userId , time .Minute * 10 ).Err (); err != nil {
381
+ log .Println ("Error extending room time on ping: " , err .Error ())
382
+ } else {
383
+
384
+ log .Printf ("expiration reset for 10 minutes for user %s: " , userId )
385
+ }
386
+
387
+ }
388
+
360
389
type ClientWorkspace struct {
361
390
Clients int `json:"clients"`
362
391
Workspace string `json:"workspace"`
0 commit comments