-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
54 lines (44 loc) · 1.5 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
const express = require('express');
const app = express();
const fs = require('fs');
const path = require('path');
const https = require('https');
const uuid = require('uuid/v4');
var key = fs.readFileSync('example-com.key.pem');
var cert = fs.readFileSync('example-com.cert.pem');
var options = {key, cert};
const server = https.createServer(options, app);
const io = require('socket.io')(server);
app.use(express.static(__dirname));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/index.html'));
});
server.listen(3000, () => {
console.log('listening on port 3000');
});
let numClients = 0;
io.on('connection', socket => {
socket.on('send join', () => {
let clientId = socket.id;
socket.broadcast.emit('receive client joined', clientId);
});
socket.on('send offer', ({offer, toClientId}) => {
let fromClientId = socket.id;
socket.to(toClientId).emit('receive offer', {offer, fromClientId});
});
socket.on('send answer', ({answer, toClientId}) => {
let fromClientId = socket.id;
socket.to(toClientId).emit('receive answer', {answer, fromClientId});
});
socket.on('send ice candidate', ({candidate, toClientId}) => {
let fromClientId = socket.id;
socket.to(toClientId).emit('receive ice candidate', {candidate, fromClientId});
});
socket.on('disconnect', () => {
let clientId = socket.id;
socket.broadcast.emit('receive client leave', clientId);
});
});
module.exports = {
app
};