Skip to content

Commit e0bf1a3

Browse files
committed
Fix the server content being wiped
1 parent 185dc0f commit e0bf1a3

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

collab/main.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
AUTH_FAIL = "auth_fail"
2424
CLOSE_SESSION = "close_session"
2525
CONTENT_CHANGE = "content_change"
26-
PING = "ping"
26+
PING = "ping"
2727
)
2828

2929
var upgrader = websocket.Upgrader{
@@ -140,7 +140,9 @@ func (h *Hub) Run() {
140140
case message := <-h.broadcast:
141141
h.mutex.Lock()
142142
// 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+
}
144146
for client := range h.clients {
145147
if client.roomID == message.RoomID {
146148

@@ -163,8 +165,6 @@ func (h *Hub) Run() {
163165
h.mutex.Unlock()
164166
}
165167

166-
167-
168168
}
169169
}
170170

@@ -306,14 +306,14 @@ func handleMessages(
306306
// if msgData["type"] == "ping" {
307307
// //receives ping from client1, need to send a ping to client2
308308
// //eventually, if present, client2 will send the ping back, which will be broadcasted back to client1.
309-
309+
310310
// userID, _ := msgData["userId"].(string)
311311
// request := Message {
312312
// RoomID: client.roomID,
313313
// UserID: userID,
314314
// Content: []byte("ping request"),
315315
// }
316-
316+
317317
// hub.broadcast <- request
318318
// }
319319

@@ -348,29 +348,28 @@ func handleMessages(
348348
} else if msgData.Type == PING {
349349
// Broadcast the message to other clients
350350
hub.broadcast <- Message{
351-
RoomID: client.roomID,
352-
Type: msgData.Type,
353-
UserID: msgData.UserID,
351+
RoomID: client.roomID,
352+
Type: msgData.Type,
353+
UserID: msgData.UserID,
354354
}
355355

356356
extendExpiryTime(msgData.UserID, persistMappings)
357357
} else {
358358
log.Printf("Unknown message type: %s", msgData.Type)
359-
}
359+
}
360360
}
361361
}
362362

363363
func extendExpiryTime(userId string, persistMappings *verify.PersistMappings) {
364-
364+
365365
ctx := context.Background()
366-
if err := persistMappings.Conn.Expire(ctx, userId, time.Minute * 10).Err(); err != nil {
366+
if err := persistMappings.Conn.Expire(ctx, userId, time.Minute*10).Err(); err != nil {
367367
log.Println("Error extending room time on ping: ", err.Error())
368368
} else {
369-
369+
370370
log.Printf("expiration reset for 10 minutes for user %s: ", userId)
371371
}
372-
373-
372+
374373
}
375374

376375
type ClientWorkspace struct {

0 commit comments

Comments
 (0)