-
Notifications
You must be signed in to change notification settings - Fork 19
/
server.js
40 lines (40 loc) · 1.37 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
const credentials = require('./credentials');
const express = require('express');
const app = express();
let broadcaster;
let server;
let port;
if (credentials.key && credentials.cert) {
const https = require('https');
server = https.createServer(credentials, app);
port = 443;
} else {
const http = require('http');
server = http.createServer(app);
port = 3000;
}
const io = require('socket.io')(server);
app.use(express.static(__dirname + '/public'));
io.sockets.on('error', e => console.log(e));
io.sockets.on('connection', function (socket) {
socket.on('broadcaster', function () {
broadcaster = socket.id;
socket.broadcast.emit('broadcaster');
});
socket.on('watcher', function () {
broadcaster && socket.to(broadcaster).emit('watcher', socket.id);
});
socket.on('offer', function (id /* of the watcher */, message) {
socket.to(id).emit('offer', socket.id /* of the broadcaster */, message);
});
socket.on('answer', function (id /* of the broadcaster */, message) {
socket.to(id).emit('answer', socket.id /* of the watcher */, message);
});
socket.on('candidate', function (id, message) {
socket.to(id).emit('candidate', socket.id, message);
});
socket.on('disconnect', function() {
broadcaster && socket.to(broadcaster).emit('bye', socket.id);
});
});
server.listen(port, () => console.log(`Server is running on port ${port}`));