forked from muaz-khan/RTCMultiConnection-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
119 lines (93 loc) · 3.36 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// http://127.0.0.1:9001
// http://localhost:9001
const fs = require('fs');
const path = require('path');
const url = require('url');
var httpServer = require('http');
const ioServer = require('socket.io');
const RTCMultiConnectionServer = require('./node_scripts/index.js');
var PORT = 9001;
var isUseHTTPs = false;
const jsonPath = {
config: 'config.json',
logs: 'logs.json'
};
const BASH_COLORS_HELPER = RTCMultiConnectionServer.BASH_COLORS_HELPER;
const getValuesFromConfigJson = RTCMultiConnectionServer.getValuesFromConfigJson;
const getBashParameters = RTCMultiConnectionServer.getBashParameters;
var config = getValuesFromConfigJson(jsonPath);
config = getBashParameters(config, BASH_COLORS_HELPER);
// if user didn't modifed "PORT" object
// then read value from "config.json"
if(PORT === 9001) {
PORT = config.port;
}
if(isUseHTTPs === false) {
isUseHTTPs = config.isUseHTTPs;
}
function serverHandler(request, response) {
// to make sure we always get valid info from json file
// even if external codes are overriding it
config = getValuesFromConfigJson(jsonPath);
config = getBashParameters(config, BASH_COLORS_HELPER);
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.write('RTCMultiConnection Socket.io Server.\n\n' + 'https://github.com/muaz-khan/RTCMultiConnection-Server\n\n' + 'npm install RTCMultiConnection-Server');
response.end();
}
var httpApp;
if (isUseHTTPs) {
httpServer = require('https');
// See how to use a valid certificate:
// https://github.com/muaz-khan/WebRTC-Experiment/issues/62
var options = {
key: null,
cert: null,
ca: null
};
var pfx = false;
if (!fs.existsSync(config.sslKey)) {
console.log(BASH_COLORS_HELPER.getRedFG(), 'sslKey:\t ' + config.sslKey + ' does not exist.');
} else {
pfx = config.sslKey.indexOf('.pfx') !== -1;
options.key = fs.readFileSync(config.sslKey);
}
if (!fs.existsSync(config.sslCert)) {
console.log(BASH_COLORS_HELPER.getRedFG(), 'sslCert:\t ' + config.sslCert + ' does not exist.');
} else {
options.cert = fs.readFileSync(config.sslCert);
}
if (config.sslCabundle) {
if (!fs.existsSync(config.sslCabundle)) {
console.log(BASH_COLORS_HELPER.getRedFG(), 'sslCabundle:\t ' + config.sslCabundle + ' does not exist.');
}
options.ca = fs.readFileSync(config.sslCabundle);
}
if (pfx === true) {
options = {
pfx: sslKey
};
}
httpApp = httpServer.createServer(options, serverHandler);
} else {
httpApp = httpServer.createServer(serverHandler);
}
RTCMultiConnectionServer.beforeHttpListen(httpApp, config);
httpApp = httpApp.listen(process.env.PORT || PORT, process.env.IP || "0.0.0.0", function() {
RTCMultiConnectionServer.afterHttpListen(httpApp, config);
});
// --------------------------
// socket.io codes goes below
ioServer(httpApp).on('connection', function(socket) {
RTCMultiConnectionServer.addSocket(socket, config);
// ----------------------
// below code is optional
const params = socket.handshake.query;
if (!params.socketCustomEvent) {
params.socketCustomEvent = 'custom-message';
}
socket.on(params.socketCustomEvent, function(message) {
socket.broadcast.emit(params.socketCustomEvent, message);
});
});