-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocketStream.js
35 lines (29 loc) · 1.17 KB
/
socketStream.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
const io = require('socket.io-client');
const stream = require('readable-stream');
const util = require('util');
function SocketStream(config) {
config = config || {};
stream.Writable.call(this, config);
this._host = config.host || (process.browser ? window.location.hostname : 'localhost');
this._port = config.port || 30001;
this._namespace = config.namespace || 'log';
this._eventName = config.eventName || 'log';
this._protocol = config.protocol || 'http:';
this.socket = io(util.format('%s//%s:%s/%s', this._protocol, this._host, this._port, this._namespace), config.io);
}
util.inherits(SocketStream, stream.Writable);
SocketStream.prototype._write = function(logMessage, encoding, done) {
if (this.socket.sendBuffer.length <= 50) {
try { logMessage = JSON.parse(logMessage); } catch (e) {} // winston pipes stringified JSONs
this.socket.emit(this._eventName, logMessage);
done();
} else {
done();
}
};
SocketStream.prototype._destroy = function(error, callback) {
this.socket.close(err => callback && callback(err || error));
};
module.exports = function(config) {
return new SocketStream(config);
};