Created
March 21, 2019 14:10
-
-
Save unktomi/0b7810fd0bd290e151eb4d9b25b44533 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Connected = {}; | |
function removeUser(conferenceId, uid, ws) | |
{ | |
const Conf = Connected[conferenceId]; | |
const a = Conf[uid]; | |
if (a.length == 1) { | |
if (Conf.numParticipants == 1) { | |
delete Connected[conferenceId]; | |
} else { | |
delete Conf[uid]; | |
Conf.numParticipants--; | |
} | |
} else { | |
a.splice(a.indexOf(ws), 1); | |
} | |
} | |
function addUser(conferenceId, uid, ws) { | |
var Conf = Connected[conferenceId]; | |
if (!Conf) { | |
Conf = {} | |
Conf.numParticipants = 0; | |
Connected[conferenceId] = Conf; | |
} | |
if (!Conf[uid]) { | |
Conf[uid] = [ws]; | |
} else { | |
Conf[uid].push(ws); | |
} | |
Conf.numParticipants++; | |
return Conf; | |
} | |
app.ws("/conf", (ws, req) => { | |
console.log(req.query); | |
const conferenceId = req.query.conferenceId; | |
const idToken = req.query.idToken; | |
return idTokenToUid(idToken).then(uid => { | |
ws.on("close", () => { | |
removeUser(conferenceId, uid, ws); | |
}); | |
const Conf = addUser(conferenceId, uid, ws); | |
ws.on("message", msg => { | |
for (var user in Conf) { | |
if (user === "numParticipants") continue; // hack | |
Conf[user].map(to_ws => { | |
if (to_ws != ws) { | |
try { | |
to_ws.send(msg); | |
} catch (err) { | |
console.log(err); | |
removeUser(conferenceId, uid, to_ws); | |
} | |
} | |
}); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment