-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.js
More file actions
182 lines (148 loc) · 4.84 KB
/
index.js
File metadata and controls
182 lines (148 loc) · 4.84 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
/* jshint node: true */
const fs = require('fs');
const http = require('http');
const path = require('path');
const urllib = require('url');
const ws = require('ws');
const nodeEnv = process.env.NODE_ENV || 'development';
const WebSocketServer = ws.Server;
function SocketPeerServer (opts) {
opts = opts || {};
if (!opts.httpServer) {
const host = opts.host || process.env.SOCKETPEER_HOST || process.env.HOST || '0.0.0.0';
const port = opts.port || process.env.SOCKETPEER_PORT || process.env.PORT || 3000;
opts.httpServer = http.createServer((req, res) => {
var url = urllib.parse(req.url).pathname;
if (nodeEnv === 'development') {
let stream;
// For demo purposes.
if (url === '/' || url === '/demo/') {
// res.writeHead(200, {'Content-Type': 'text/html'});
stream = fs.createReadStream(path.join(__dirname, '..', 'demo', 'index.html'));
stream.pipe(res);
}
if (url === '/media/') {
// res.writeHead(200, {'Content-Type': 'text/html'});
stream = fs.createReadStream(path.join(__dirname, '..', 'demo', 'media.html'));
stream.pipe(res);
}
}
});
opts.httpServer.listen(port, host, () => {
console.log('[%s] Server listening on %s:%s', nodeEnv, host, port);
});
}
if (typeof opts.serveLibrary === 'undefined' || opts.serveLibrary) {
opts.httpServer.on('request', (req, res) => {
var url = urllib.parse(req.url).pathname;
if (url === '/socketpeer/socketpeer.js') {
res.writeHead(200, {'Content-Type': 'text/javascript'});
var stream = fs.createReadStream(path.join(__dirname, '..', 'socketpeer.js'));
stream.pipe(res);
}
});
}
if (!opts.wsServer) {
opts.wsServer = new WebSocketServer({
server: opts.httpServer,
path: '/socketpeer/'
});
}
var peersWaiting = Object.create(null);
var connections = Object.create(null);
function closeConnection (pairCode, filter) {
if (!filter) {
// Create a no-op function.
filter = x => x;
}
let matched = false;
connections[pairCode].forEach(conn => {
matched = filter(conn);
if (matched) {
conn.peer = null;
}
});
if (matched) {
connections[pairCode] = null;
}
}
function sendMessage (type, data) {
this.send(JSON.stringify({
type: type,
data: data
}));
}
opts.wsServer.on('connection', client => {
client.sendMessage = sendMessage.bind(client);
console.log('Connection');
client.on('message', msg => {
console.log('[message] Received message: %s', msg);
var obj = JSON.parse(msg);
client.emit('message.' + obj.type, obj.data);
});
client.on('message.data', data => {
console.log('[pair] Received data:', data);
if (client.peer) {
client.peer.sendMessage('data', data);
}
});
client.on('message.pair', pairCode => {
console.log('[pair] Received pairCode:', pairCode);
if (connections[pairCode]) {
client.sendMessage('warning', {
message: '`pairCode` "' + pairCode + '" is already in use',
pairCode: pairCode
});
closeConnection(pairCode, function (clientToCheck) {
return clientToCheck !== client;
});
}
client.pairCode = pairCode;
var waiting = peersWaiting[pairCode];
if (waiting && waiting !== client) {
console.log('[pair] Other peer found');
client.peer = waiting;
waiting.peer = client;
connections[pairCode] = [client, waiting];
peersWaiting[pairCode] = null;
waiting.sendMessage('peer.found', {initiator: false});
client.sendMessage('peer.found', {initiator: true});
} else {
console.log('[pair] No other peer found');
// I am waiting for you.
peersWaiting[pairCode] = client;
}
});
client.on('message.rtc.signal', data => {
console.log('[rtc.signal] Signal recieved');
if (client.peer) {
client.peer.sendMessage('rtc.signal', data);
} else {
console.warn('[rtc.signal] Signal with no peer!');
}
});
client.on('message.rtc.connect', () => {
console.log('[rtc.connect] Received');
if (client.peer) {
client.peer.sendMessage('rtc.connect');
}
});
client.on('close', () => {
const pairCode = client.pairCode;
if (pairCode in peersWaiting && peersWaiting[pairCode] === client) {
peersWaiting[pairCode] = null;
}
if (client.peer) {
peersWaiting[pairCode] = client.peer;
closeConnection(pairCode);
}
});
});
return opts.httpServer;
}
// Immediately start the server if the server is called directly
// (i.e., not required as a module).
if (require.main === module) {
SocketPeerServer();
}
module.exports = SocketPeerServer;