forked from nikatlas/ShareFilesOnLANNode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
waiter.js
65 lines (61 loc) · 1.72 KB
/
waiter.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
var os = require('os');
var fs = require('fs');
var ss = require('socket.io-stream');
var path = require('path');
var mee = require('multicast-eventemitter');
var emitter = mee.getEmitter();
var name = process.argv[2];
var port = parseInt(process.argv[3]);
var ip = getMyIp();
console.log('My Ip = ' + ip);
//if folder is specified
var upFolder = "upload";
if (process.argv.length >= 5){
upFolder = process.argv[4];
}
// Stay ALIVE
setupEmitter();
// IO Socket
var io = require('socket.io')();
io.on('connection', function(socket){
console.log('New connection');
ss(socket).on('file', function(stream, data) {
var filename = upFolder + "/" + path.basename(data.name);
console.log("> Saving File > " + filename);
console.log("File is coming... Filename:" + filename);
stream.pipe(fs.createWriteStream(filename));
});
socket.on('disconnect', function(){
console.log('disconnect');
});
});
io.listen(port);
console.log("IO Server is up and listening on port " + port );
function getAddresses(){
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
return addresses;
}
function getMyIp(){
var addresses = getAddresses();
if( addresses.length == 0 )
throw "Address not found!";
return addresses[0];
}
function setupEmitter(){
emitter.on('anyonethere', function(text, time) {
ip = getMyIp();
console.log('My Ip = ' + ip);
var now = new Date().getTime();
emitter.emit('imhere', name+","+ip+","+port , now );
console.log("Response to query: " + text + " / " + time );
});
}