Skip to content

Commit

Permalink
socket: flush the write buffer before closing the socket (fixes socke…
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Oct 25, 2014
1 parent 0cc7cdf commit e754f7e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
25 changes: 19 additions & 6 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,24 @@ Socket.prototype.getAvailableUpgrades = function () {
*/

Socket.prototype.close = function () {
if ('open' == this.readyState) {
this.readyState = 'closing';
var self = this;
this.transport.close(function () {
self.onClose('forced close');
});
if ('open' != this.readyState) return;

this.readyState = 'closing';

if (this.writeBuffer.length) {
this.once('drain', this.closeTransport.bind(this));
return;
}

this.closeTransport();
};

/**
* Closes the underlying transport.
*
* @api private
*/

Socket.prototype.closeTransport = function () {
this.transport.close(this.onClose.bind(this, 'forced close'));
};
30 changes: 29 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ describe('server', function () {
});
});

it('should arrive from server to client (ws)', function (done) {
it('should arrive from server to client (multiple, ws)', function (done) {
var opts = { allowUpgrades: false, transports: ['websocket'] };
var engine = listen(opts, function (port) {
var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] })
Expand Down Expand Up @@ -935,6 +935,34 @@ describe('server', function () {
});
});

it('should arrive from server to client (multiple, no delay, ws)', function (done) {
var opts = { allowUpgrades: false, transports: ['websocket'] };
var engine = listen(opts, function (port) {
var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] })
, expected = ['a', 'b', 'c']
, i = 0;

engine.on('connection', function (conn) {
conn.on('close', function () {
setTimeout(function () {
expect(i).to.be(3);
done();
}, 50);
});
conn.send('a');
conn.send('b');
conn.send('c');
conn.close();
});

socket.on('open', function () {
socket.on('message', function (msg) {
expect(msg).to.be(expected[i++]);
});
});
});
});

it('should arrive when binary data is sent as Int8Array (ws)', function (done) {
var binaryData = new Int8Array(5);
for (var i = 0; i < binaryData.length; i++) {
Expand Down

0 comments on commit e754f7e

Please sign in to comment.