-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
executable file
·174 lines (142 loc) · 6.54 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
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
// Muaz Khan - www.MuazKhan.com
// MIT License - www.WebRTC-Experiment.com/licence
// Documentation - github.com/muaz-khan/RTCMultiConnection
var isUseHTTPs = false && !(!!process.env.PORT || !!process.env.IP);
var port = process.env.PORT || 9001;
try {
var _port = require('./config.json').port;
if (_port && _port.toString() !== '9001') {
port = parseInt(_port);
}
} catch (e) {}
var server = require(isUseHTTPs ? 'https' : 'http'),
url = require('url'),
path = require('path'),
fs = require('fs');
function serverHandler(request, response) {
try {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
if (filename && filename.search(/server.js|Scalable-Broadcast.js|Signaling-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);
if (filename && filename.search(/FRD/g) === -1 && stats.isDirectory()) {
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.write('<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0;url=/FRD/"></head><body></body></html>');
response.end();
return;
}
} 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'
});
if (filename.indexOf('/FRD/MultiRTC/') !== -1) {
filename = filename.replace('/FRD/MultiRTC/', '');
filename += '/FRD/MultiRTC/index.html';
} else if (filename.indexOf('/FRD/') !== -1) {
filename = filename.replace('/FRD/', '');
filename += '/FRD/index.html';
} else {
filename += '/FRD/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;
}
try {
var FRD = (fs.readdirSync('FRD') || []);
if (FRD.length) {
var h2 = '<h2 style="text-align:center;display:block;"><a href="https://www.npmjs.com/package/rtcmulticonnection-v3"><img src="https://img.shields.io/npm/v/rtcmulticonnection-v3.svg"></a><a href="https://www.npmjs.com/package/rtcmulticonnection-v3"><img src="https://img.shields.io/npm/dm/rtcmulticonnection-v3.svg"></a><a href="https://travis-ci.org/muaz-khan/RTCMultiConnection"><img src="https://travis-ci.org/muaz-khan/RTCMultiConnection.png?branch=master"></a></h2>';
var otherFRD = '<section class="experiment" id="FRD"><details><summary style="text-align:center;">Check ' + (FRD.length - 1) + ' other RTCMultiConnection-v3 FRD</summary>' + h2 + '<ol>';
FRD.forEach(function(f) {
if (f && f !== 'index.html' && f.indexOf('.html') !== -1) {
otherFRD += '<li><a href="/FRD/' + f + '">' + f + '</a> (<a href="https://github.com/muaz-khan/RTCMultiConnection/tree/master/FRD/' + f + '">Source</a>)</li>';
}
});
otherFRD += '<ol></details></section><section class="experiment own-widgets latest-commits">';
file = file.replace('<section class="experiment own-widgets latest-commits">', otherFRD);
}
} catch (e) {}
try {
var docs = (fs.readdirSync('docs') || []);
if (docs.length) {
var html = '<section class="experiment" id="docs">';
html += '<h2><a href="#docs">Documentation</a></h2>';
html += '<ol>';
docs.forEach(function(f) {
html += '<li><a href="https://github.com/muaz-khan/RTCMultiConnection/tree/master/docs/' + f + '">' + f + '</a></li>';
});
html += '</ol></section><section class="experiment own-widgets latest-commits">';
file = file.replace('<section class="experiment own-widgets latest-commits">', html);
}
} catch (e) {}
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 app;
if (isUseHTTPs) {
var options = {
key: fs.readFileSync(path.join(__dirname, 'fake-keys/privatekey.pem')),
cert: fs.readFileSync(path.join(__dirname, 'fake-keys/certificate.pem'))
};
app = server.createServer(options, serverHandler);
} else app = server.createServer(serverHandler);
app = app.listen(port, process.env.IP || '0.0.0.0', function() {
var addr = app.address();
if (addr.address === '0.0.0.0') {
addr.address = 'localhost';
}
console.log('Server listening at ' + (isUseHTTPs ? 'https' : 'http') + '://' + addr.address + ':' + addr.port);
});
require('./Signaling-Server.js')(app, function(socket) {
try {
var params = socket.handshake.query;
// "socket" object is totally in your own hands!
// do whatever you want!
// in your HTML page, you can access socket as following:
// connection.socketCustomEvent = 'custom-message';
// var socket = connection.getSocket();
// socket.emit(connection.socketCustomEvent, { test: true });
if (!params.socketCustomEvent) {
params.socketCustomEvent = 'custom-message';
}
socket.on(params.socketCustomEvent, function(message) {
try {
socket.broadcast.emit(params.socketCustomEvent, message);
} catch (e) {}
});
} catch (e) {}
});