forked from holtwick/briefing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rooms.js
50 lines (43 loc) · 1.01 KB
/
rooms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const log = require("debug")("signal:rooms")
let rooms = {}
function addProperty(obj, key, value, info = true) {
if (key == null) return false
let values = obj[key]
if (values == null) {
values = {}
obj[key] = values
}
const created = values[value] == null
values[value] = info
return created
}
function removeProperty(obj, key, value) {
let removed = false
if (key != null) {
let values = obj[key]
if (values != null) {
removed = values[value] != null
delete values[value]
}
}
return removed
}
function addSocketToRoom(sid, room) {
log("addSocketToRoom", sid, room)
return addProperty(rooms, room, sid)
}
function removeSocketFromRoom(sid, room) {
log("removeSocketFromRoom", sid, room)
return removeProperty(rooms, room, sid)
}
function allSocketsForRoom(room) {
const sockets = Object.keys(rooms[room] || {})
log("allSocketsForRoom", room, sockets)
return sockets
}
module.exports = {
rooms,
addSocketToRoom,
removeSocketFromRoom,
allSocketsForRoom,
}