|
|
@@ -1,18 +1,22 @@
|
|
1
|
1
|
|
|
2
|
|
-var STREAM_PORT = 8082,
|
|
3
|
|
- STREAM_SECRET = 's3cret', // CHANGE THIS!
|
|
4
|
|
- WEBSOCKET_PORT = 8084,
|
|
|
2
|
+if( process.argv.length < 3 ) {
|
|
|
3
|
+ console.log(
|
|
|
4
|
+ 'Usage: \n' +
|
|
|
5
|
+ 'node stream-server.js <secret> [<stream-port> <websocket-port>]'
|
|
|
6
|
+ );
|
|
|
7
|
+ process.exit();
|
|
|
8
|
+}
|
|
|
9
|
+
|
|
|
10
|
+var STREAM_SECRET = process.argv[2],
|
|
|
11
|
+ STREAM_PORT = process.argv[3] || 8082,
|
|
|
12
|
+ WEBSOCKET_PORT = process.argv[4] || 8084,
|
|
5
|
13
|
STREAM_MAGIC_BYTES = 'jsmp'; // Must be 4 bytes
|
|
6
|
14
|
|
|
7
|
|
-var clients = {};
|
|
8
|
15
|
var width = 320,
|
|
9
|
16
|
height = 240;
|
|
10
|
17
|
|
|
11
|
18
|
// Websocket Server
|
|
12
|
19
|
var socketServer = new (require('ws').Server)({port: WEBSOCKET_PORT});
|
|
13
|
|
-var _uniqueClientId = 1;
|
|
14
|
|
-
|
|
15
|
|
-var socketError = function() { /* ignore */ };
|
|
16
|
20
|
socketServer.on('connection', function(socket) {
|
|
17
|
21
|
// Send magic bytes and video size to the newly connected socket
|
|
18
|
22
|
// struct { char magic[4]; unsigned short width, height;}
|
|
|
@@ -20,26 +24,21 @@ socketServer.on('connection', function(socket) {
|
|
20
|
24
|
streamHeader.write(STREAM_MAGIC_BYTES);
|
|
21
|
25
|
streamHeader.writeUInt16BE(width, 4);
|
|
22
|
26
|
streamHeader.writeUInt16BE(height, 6);
|
|
23
|
|
- socket.send(streamHeader, {binary:true}, socketError);
|
|
|
27
|
+ socket.send(streamHeader, {binary:true});
|
|
24
|
28
|
|
|
25
|
|
- // Remember client in 'clients' object
|
|
26
|
|
- var clientId = _uniqueClientId++;
|
|
27
|
|
- clients[clientId] = socket;
|
|
28
|
|
- console.log(
|
|
29
|
|
- 'WebSocket Connect: client #' + clientId +
|
|
30
|
|
- ' ('+Object.keys(clients).length+' total)'
|
|
31
|
|
- );
|
|
32
|
|
-
|
|
33
|
|
- // Delete on close
|
|
|
29
|
+ console.log( 'New WebSocket Connection ('+socketServer.clients.length+' total)' );
|
|
|
30
|
+
|
|
34
|
31
|
socket.on('close', function(code, message){
|
|
35
|
|
- delete clients[clientId];
|
|
36
|
|
- console.log(
|
|
37
|
|
- 'WebSocket Disconnect: client #' + clientId +
|
|
38
|
|
- ' ('+Object.keys(clients).length+' total)'
|
|
39
|
|
- );
|
|
|
32
|
+ console.log( 'Disconnected WebSocket ('+socketServer.clients.length+' total)' );
|
|
40
|
33
|
});
|
|
41
|
34
|
});
|
|
42
|
35
|
|
|
|
36
|
+socketServer.broadcast = function(data, opts) {
|
|
|
37
|
+ for( var i in this.clients ) {
|
|
|
38
|
+ this.clients[i].send(data, opts);
|
|
|
39
|
+ }
|
|
|
40
|
+};
|
|
|
41
|
+
|
|
43
|
42
|
|
|
44
|
43
|
// HTTP Server to accept incomming MPEG Stream
|
|
45
|
44
|
var streamServer = require('http').createServer( function(request, response) {
|
|
|
@@ -53,9 +52,7 @@ var streamServer = require('http').createServer( function(request, response) {
|
|
53
|
52
|
':' + request.socket.remotePort + ' size: ' + width + 'x' + height
|
|
54
|
53
|
);
|
|
55
|
54
|
request.on('data', function(data){
|
|
56
|
|
- for( c in clients ) {
|
|
57
|
|
- clients[c].send(data, {binary:true}, socketError);
|
|
58
|
|
- }
|
|
|
55
|
+ socketServer.broadcast(data, {binary:true});
|
|
59
|
56
|
});
|
|
60
|
57
|
}
|
|
61
|
58
|
else {
|