Skip to content

Commit

Permalink
socket: use stream2 Readable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln committed Feb 28, 2017
1 parent 2004786 commit c8f8bfd
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/transport.socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function SocketTransport(socket) {
this.remoteAddress = socket.remoteAddress;

this.socket.setEncoding('utf8');
this.socket.on('data', this._onSocketData.bind(this));
this.socket.on('readable', this._onSocketReadable.bind(this));

common.forwardMultipleEvents(this.socket, this, [
'error',
Expand Down Expand Up @@ -212,12 +212,18 @@ SocketTransport.prototype.end = function(data) {
this.socket.destroy();
};

SocketTransport.prototype._onSocketReadable = function() {
let chunk = null;
while ((chunk = this.socket.read()) !== null)
this._buffer += chunk;
this._processData();
};

// Socket data handler
// data - data received
//
SocketTransport.prototype._onSocketData = function(chunk) {
SocketTransport.prototype._processData = function() {
const packets = [];
this._buffer += chunk;

try {
this._buffer = jsrs.parseNetworkPackets(this._buffer, packets);
Expand Down

0 comments on commit c8f8bfd

Please sign in to comment.