-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
55 lines (42 loc) · 1.38 KB
/
server.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
51
52
53
54
55
//requires
const express = require('express');
const app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
// express routing
app.use(express.static('public'));
// signaling
io.on('connection', function (socket) {
console.log('a user connected');
socket.on('create or join', function (room) {
console.log('create or join to room ', room);
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);
}
});
socket.on('ready', function (room){
socket.broadcast.to(room).emit('ready');
});
socket.on('candidate', function (event){
socket.broadcast.to(event.room).emit('candidate', event);
});
socket.on('offer', function(event){
socket.broadcast.to(event.room).emit('offer',event.sdp);
});
socket.on('answer', function(event){
socket.broadcast.to(event.room).emit('answer',event.sdp);
});
});
// listener
http.listen(3000, function () {
console.log('listening on *:3000');
});