-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkStreamer.js
More file actions
executable file
·36 lines (28 loc) · 908 Bytes
/
Copy pathNetworkStreamer.js
File metadata and controls
executable file
·36 lines (28 loc) · 908 Bytes
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
var util = require('util');
var Writable = require('stream').Writable;
util.inherits(NetworkStreamer, Writable);
/**
NetworkStreamer inherits from writable stream. In takes an socket.io connection as the parameter,
and whenever new data arrives to this stream, it emits it in the 'streamBuffer' event.
It is used to send whole file streams to the client using socket.io.
*/
function NetworkStreamer(socket, options)
{
if(!(this instanceof NetworkStreamer))
return new NetworkStreamer(socket, options);
Writable.call(this, options);
this.socket = socket;
this.totalSize = 0;
};
NetworkStreamer.prototype._write = function(chunk, encoding, callback)
{
this.totalSize += chunk.length;
this.socket.emit('streamBuffer', {"buffer" : chunk}, function(){
callback();
});
};
NetworkStreamer.prototype.getTotalSize = function()
{
return this.totalSize;
};
exports.NetworkStreamer = NetworkStreamer;