-
Notifications
You must be signed in to change notification settings - Fork 3
/
streamer.js
47 lines (43 loc) · 1.42 KB
/
streamer.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
var listenSocket;
var bus;
var opts = {};
function startStreamer() {
myPort = opts.port || 8080;
mainServer = opts.host || 'localhost:40996/';
myRole = opts.role || 'slave';
var WebSocket = require('ws');
var ws = new WebSocket('ws://' + mainServer);
ws.on('open', function open() {
ws.send(JSON.stringify({class: 'node',
type: 'streamer',
role: myRole,key: opts.key, }));
});
ws.on('message', function(data, flags) {
switch (data.function) {
case 'register': {
if (data.message == 'success') {
// Create main Stream server; listens for boot client connects
listenSocket = require('socket.io').listen(myPort);
// Connect to Main Server for Comm
listenSocket.of('/connect').on('connection', function(socket) {
// The Upload Function
ss(socket).on('put', function(stream, data) {
var filename = path.basename(data.file);
stream.pipe(fs.createWriteStream(filename));
});
// The Get function
socket.of('get', function(data) {
var filename = path.basename(data.file);
ss(socket).emit('get', stream, {name: filename});
fs.createReadStream(filename).pipe(str).pipe(stream);
});
});
}
}
}
});
}
opts.host = process.argv[2];
opts.key = process.argv[3];
opts.role = process.argv[4];
startStreamer();