Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions backend/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,27 +345,28 @@ func WebsocketHandler(c *gin.Context) {
client.SafeWriteJSON(participantsMsg)

// Send existing participants' detailed info to the new client
for connRef, existing := range room.Clients {
payload := map[string]interface{}{
"id": existing.UserID,
"username": existing.Username,
"displayName": existing.Username,
"email": existing.Email,
"avatarUrl": existing.AvatarURL,
"elo": existing.Elo,
}
detailMessage := map[string]interface{}{
"type": "userDetails",
"userDetails": payload,
}

if connRef == conn {
// Already sent this client's participant data; ensure they have their own detail payload too
client.SafeWriteJSON(detailMessage)
} else {
// Send existing participant info to the new client
client.SafeWriteJSON(detailMessage)
}
// Snapshot existing participants' detail payloads under the lock,
// then write outside the lock to avoid a data race on room.Clients
// and to avoid holding the mutex during network I/O.
room.Mutex.Lock()
detailMessages := make([]map[string]interface{}, 0, len(room.Clients))
for _, existing := range room.Clients {
detailMessages = append(detailMessages, map[string]interface{}{
"type": "userDetails",
"userDetails": map[string]interface{}{
"id": existing.UserID,
"username": existing.Username,
"displayName": existing.Username,
"email": existing.Email,
"avatarUrl": existing.AvatarURL,
"elo": existing.Elo,
},
})
}
room.Mutex.Unlock()

for _, detailMessage := range detailMessages {
client.SafeWriteJSON(detailMessage)
}

// Prepare detailed payload for the new client to broadcast to others
Expand Down
Loading