-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
104 lines (85 loc) · 2.37 KB
/
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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const Express = require("express");
const CORS = require("cors");
const FS = require("fs");
const HTTPS = require("https");
const Path = require("path");
const WebSocketServer = require ("websocket").server;
//Get the Medooze Media Server interface
const MediaServer = require("medooze-media-server");
const PORT = 8084;
const letsencrypt = false;
//Check
if (process.argv.length!=3)
throw new Error("Missing IP address\nUsage: node index.js <ip>");
//Get ip
const ip = process.argv[2];
//Restrict port range
MediaServer.setPortRange(10000,10100);
//Create UDP server endpoint
const endpoint = MediaServer.createEndpoint(ip);
//Enable debug
MediaServer.enableDebug(false);
MediaServer.enableUltraDebug(false);
//Create rest api
const rest = Express();
rest.use(CORS());
rest.use(Express.static("www"));
// Load the demo handlers
const handlers = {
"simulcast" : require("./lib/simulcast.js"),
"transceivers" : require("./lib/PeerConnectionServerDemo.js"),
"partyline" : require("./lib/PartyLine.js"),
"twcc" : require("./lib/twcc.js"),
};
function wss(server)
{
//Create websocket server
const wssServer = new WebSocketServer ({
httpServer: server,
autoAcceptConnections: false
});
wssServer.on ("request", (request) => {
//Get protocol for demo
var protocol = request.requestedProtocols[0];
console.log ("-Got request for: " + protocol);
//If nor found
if (!handlers.hasOwnProperty (protocol))
//Reject connection
return request.reject ();
//Process it
handlers[protocol] (request, protocol, endpoint);
});
}
//Create HTTP server
if (letsencrypt)
{
//Use greenlock to get ssl certificate
const gle = require("greenlock-express").init({
packageRoot: __dirname,
configDir: "./greenlock.d",
maintainerEmail : "[email protected]",
cluster: false
});
gle.ready((gle)=>wss(gle.httpsServer()));
gle.serve(rest);
} else {
//Load certs
const options = {
key : FS.readFileSync ("server.key"),
cert : FS.readFileSync ("server.cert")
};
//Manualy starty server
const server = HTTPS.createServer (options, rest).listen(PORT);
//Launch wss server
wss(server);
}
//Try to clean up on exit
const onExit = (e) => {
if (e) console.error(e);
MediaServer.terminate();
process.exit();
};
process.on("uncaughtException" , onExit);
process.on("SIGINT" , onExit);
process.on("SIGTERM" , onExit);
process.on("SIGQUIT" , onExit);