-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
57 lines (48 loc) · 1.38 KB
/
client.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
var io = require('socket.io-client');
var ss = require('socket.io-stream');
var fs = require('fs');
var commander = require('commander');
commander.version('0.0.1')
.option('-u, --upload', 'Upload the given file')
.option('-d, --download', 'Downoad the file')
.parse(process.argv);
var uploadFile = function(file) {
fs.stat(file, function(err, stats) {
if (err || !stats) {
console.error("file not found");
process.exit(1);
}
else {
var sizeFile = stats.size;
var currentSizeUploaded = 0;
ss(socket).emit('file', stream, {name: filename});
fs.createReadStream(filename).pipe(stream);
socket.on('uploaded-size', function(size) {
console.log("size uploaded : " + size);
currentSizeUploaded += size;
var currentPercent = currentSizeUploaded / sizeFile * 100;
console.log(currentPercent);
});
}
});
}
if (!commander.upload && !commander.download) {
console.log("no options specified");
process.exit(1);
}
else {
var socket = io.connect('http://127.0.0.1:9000');
var stream = ss.createStream();
var filename = 'file.txt';
socket.on('connect', function() {
console.log("client connected");
if (commander.upload) {
uploadFile('file.txt');
}
else if (commande.download) {
}
socket.on('disconnect', function() {
console.log("disctonned to the server");
});
});
}