-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (28 loc) · 922 Bytes
/
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
//requires
const express = require('express');
const app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const port = process.env.PORT || 3000;
// signaling
io.on('connection', function (socket) {
console.log('a user connected to websocket');
socket.on('create a room', function (room) {
console.log('user created a room', room);
//get a room add to database
var myRoom = io.sockets.adapter.rooms[room] || { length: 0 };
var numClients = myRoom.length;
console.log(room, ' has ', numClients, ' clients');
if (numClients == 0) {
socket.join(room);
socket.emit('created', room);
} else if (numClients == 1) {
socket.join(room);
socket.emit('joined', room);
} else {
socket.emit('full', room);
}
});
});
// listener
http.listen(port);