chat-dev / server /helpers.js
incognitolm's picture
Initial
5383ef0 verified
raw
history blame contribute delete
560 Bytes
// 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));
}