-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
43 lines (38 loc) · 1.21 KB
/
index.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
"use strict";
var server = require("http").createServer();
var io = require("socket.io")(server);
var p2pserver = require("socket.io-p2p-server").Server;
var CoreLogger = require("core-logger");
// log to console
var logger = new CoreLogger({
label: "socket.io-p2p-room-server"
});
var maxRoomClients = 5;
server.listen(3030);
io.on("connection", function(socket){
socket.on("create-room", function(roomId) {
logger.info("created Room", {roomId: roomId});
p2pserver(socket, null, {name: roomId});
});
socket.on("join-room", function(roomId){
logger.info("attempting to join room", {userId: socket.id, roomId: roomId});
if (roomId in io.sockets.adapter.rooms) {
var room = io.sockets.adapter.rooms[roomId];
var numClients = Object.keys(room).length;
if (numClients >= maxRoomClients) {
logger.info("room is full", {userId: socket.id, roomId: roomId});
socket.emit("full-room");
} else {
socket.join(roomId);
p2pserver(socket, null, {name: roomId});
logger.info("room joined", {
userId: socket.id,
roomId: roomId,
numClients: numClients
});
}
} else {
socket.emit("invalid-room");
}
});
});