-
Notifications
You must be signed in to change notification settings - Fork 4
/
Server.js
67 lines (52 loc) · 2.02 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
56
57
58
59
60
61
62
63
64
65
66
67
// jshint node: true
'use strict';
const http = require('http');
const express = require('express');
const websocket = require('websocket');
const staticFileServer = express();
staticFileServer.use('/', express.static(`${__dirname}/public`));
staticFileServer.use('/offerer', express.static(`${__dirname}/public/offerer.html`));
staticFileServer.use('/answerer', express.static(`${__dirname}/public/answerer.html`));
const httpServer = http.createServer(staticFileServer);
const webSocket = new websocket.server({
httpServer: httpServer
});
let offerer;
let answerer;
webSocket.on('request', (request) => {
const connection = request.accept('json', request.origin);
connection.on('message', (data) => {
const payload = JSON.parse(data.utf8Data);
if (payload.type === 'registerAnswerer') {
answerer = connection;
console.info('Recorded new answerer.');
}
if (payload.type === 'offer') {
offerer = connection;
if (!!answerer) {
answerer.send(JSON.stringify(payload));
console.info('Sending offer to answerer after recording new offerer.');
}
}
if (payload.type === 'answer') {
if (!!offerer) {
offerer.send(JSON.stringify(payload));
console.info('Sending answer to offerer after receiving answer.');
}
}
if (payload.type === 'ICE-offerer') {
if (!!answerer) {
answerer.send(JSON.stringify({type: 'ICE', candidate: payload.candidate}));
console.info('Sending answerer an ICE candidate.');
}
}
if (payload.type === 'ICE-answerer') {
if (!!offerer) {
offerer.send(JSON.stringify({type: 'ICE', candidate: payload.candidate}));
console.info('Sending offerer an ICE candidate.');
}
}
});
});
const port = 8085;
httpServer.listen(port, () => console.info('Server has started on port:', port));