File size: 560 Bytes
5383ef0
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// helpers.js — safeSend and broadcastToUser without importing from index
// (avoids circular dependency index→wsHandler→index)
export function safeSend(ws, data) {
  if (ws.readyState === 1) ws.send(JSON.stringify(data));
}

// broadcastToUser needs access to wsClients which lives in index.
// We pass it as a parameter instead.
export function broadcastToUser(wsClients, userId, data, excludeWs = null) {
  for (const [ws, c] of wsClients)
    if (c.userId === userId && ws !== excludeWs && ws.readyState === 1)
      ws.send(JSON.stringify(data));
}