-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-2.js
More file actions
207 lines (161 loc) · 5.41 KB
/
server-2.js
File metadata and controls
207 lines (161 loc) · 5.41 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// http://127.0.0.1:8080
// http://localhost:8080
const fs = require('fs');
const path = require('path');
const url = require('url');
var events = require('events');
var httpServer = require('http');
var ioServer = require('socket.io');
var socket = require('socket.io-client');
const RTCMultiConnectionServer = require('rtcmulticonnection-server');
var PORT = 8080;
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 === 8080) {
PORT = config.port;
}
if(isUseHTTPs === false) {
isUseHTTPs = config.isUseHTTPs;
}
/*
var fs = require('fs');
require('http').createServer(function(req, res) {
if (req.url === '/page.html') {
res.end(fs.readFileSync('./page.html').toString());
}
else if (req.url === '/unautrepage.html') {
res.end(fs.readFileSync('./unautrepage.html').toString());
}
else {
res.end('not found');
}
}).listen(3000);
*/
function serverHandler(request, response) {
try {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
var routes = ['/play.html', '/listen.html', '/assets/play.js', '/assets/listen.js'];
if (routes.indexOf(page) !== -1) {
res.writeHead(200);
fs.readFile(page, 'utf-8', function(error, content) {
res.end(content);
});
if (filename && filename.search(/server.js/g) !== -1) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write('404 Not Found: ' + path.join('/', uri) + '\n');
response.end();
return;
}
var stats;
try {
stats = fs.lstatSync(filename);
} catch (e) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write('404 Not Found: ' + path.join('/', uri) + '\n');
response.end();
return;
}
if (fs.statSync(filename).isDirectory()) {
response.writeHead(404, {
'Content-Type': 'text/html'
});
filename += '/index.html';
}
fs.readFile(filename, 'utf8', function(err, file) {
if (err) {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.write('404 Not Found: ' + path.join('/', uri) + '\n');
response.end();
return;
}
response.writeHead(200);
response.write(file, 'utf8');
response.end();
});
} catch (e) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write('<h1>Unexpected error:</h1><br><br>' + e.stack || e.message || JSON.stringify(e));
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: config });
socket.on('clickedSend', function(roomId) {
console.log(roomId);
//send a message to ALL connected clients
socket.broadcast.emit('clickedReceived', roomId);
});
// ----------------------
// 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);
});
});
*/