Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ Transport.prototype.close = function () {
* @api private
*/

Transport.prototype.send = function(packets){
Transport.prototype.send = function(packets, fn){
if ('open' == this.readyState) {
this.write(packets);
this.write(packets, fn);
} else {
throw new Error('Transport not open');
}
Expand Down Expand Up @@ -137,5 +137,6 @@ Transport.prototype.onPacket = function (packet) {

Transport.prototype.onClose = function () {
this.readyState = 'closed';
this.intervalCleanup && this.intervalCleanup.call(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This belongs in the WS onClose

this.emit('close');
};
26 changes: 24 additions & 2 deletions lib/transports/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var global = util.global();

function WS(opts){
Transport.call(this, opts);
this.bufferedAmountId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this

};

/**
Expand Down Expand Up @@ -82,11 +83,32 @@ WS.prototype.doOpen = function(){
*/

WS.prototype.write = function(packets){
for (var i = 0, l = packets.length; i < l; i++) {
this.socket.send(parser.encodePacket(packets[i]));
var self = this;
// encodePayload instead of encodePacket
this.writable = false;
this.socket.send(parser.encodePayload(packets));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encodePacket is more efficient in this case so we can leverage the WS framing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(we should add a comment for that)

// check periodically if we're done sending
if ('bufferedAmount' in this.socket) {
this.bufferedAmountId = this.setInterval(function() {
if (this.socket.bufferedAmount == 0) {
clearInterval(self.bufferedAmountId);
this.writable = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this?

this.emit('drain');
}
}, 50);
} else {
// fake drain
this.writable = true;
this.emit('drain');
}
};

WS.prototype.intervalCleanup = function(){
if (this.bufferedAmountId) {
clearInterval(this.bufferedAmountId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for if.

}
}

/**
* Closes socket.
*
Expand Down