-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
35 lines (32 loc) · 869 Bytes
/
socket.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
var fs = require('fs');
var ss = require('socket.io-stream');
var io = require('socket.io')();
module.exports = function(server) {
io.on('connection', function(socket){
console.log("client connected");
socket.on('disconnect', function() {
disconnectSocket()
});
ss(socket).on('file', function(stream) {
uploadFile(socket, stream)
});
});
io.listen(server);
}
var disconnectSocket = function() {
console.log("client disconnected");
}
var uploadFile = function(socket, stream) {
console.log("get stream upload");
var path = 'file.txt';
var writeStream = fs.createWriteStream(path)
stream.pipe(writeStream);
stream.on('data', function(data) {
console.log(data);
io.emit('uploaded-size', data.length);
});
writeStream.on('finish', function(){
console.log("upload over");
socket.disconnect();
});
}