Skip to content

Fixed bugs in the previous send callback fix and added new test cases #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 16, 2013
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 19 additions & 5 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function Socket (id, server, transport) {
this.readyState = 'opening';
this.writeBuffer = [];
this.packetsFn = [];
this.sentCallbackFn = [];

this.setTransport(transport);
this.onOpen();
Expand Down Expand Up @@ -226,6 +227,7 @@ Socket.prototype.clearTransport = function () {
Socket.prototype.onClose = function (reason, description) {
if ('closed' != this.readyState) {
this.packetsFn = [];
this.sentCallbackFn = [];
this.clearTransport();
this.readyState = 'closed';
this.emit('close', reason, description);
Expand All @@ -242,11 +244,18 @@ Socket.prototype.setupSendCallback = function () {
var self = this;
//the message was sent successfully, execute the callback
this.transport.on('drain', function() {
if (self.packetsFn.length > 0) {
var seqFn = self.packetsFn.splice(0,1)[0];
if (self.sentCallbackFn.length > 0) {
var seqFn = self.sentCallbackFn.splice(0,1)[0];
if ('function' == typeof seqFn) {
debug('executing send callback');
seqFn(self.transport);
} else if (Array.isArray(seqFn)) {
debug('executing batch send callback');
for (var i in seqFn) {
if ('function' == typeof seqFn[i]) {
seqFn[i](self.transport);
}
}
}
}
});
Expand Down Expand Up @@ -288,9 +297,8 @@ Socket.prototype.sendPacket = function (type, data, callback) {
this.writeBuffer.push(packet);

//add send callback to object
if (callback) {
this.packetsFn.push(callback);
}
this.packetsFn.push(callback);

this.flush();
}
};
Expand All @@ -309,6 +317,12 @@ Socket.prototype.flush = function () {
this.server.emit('flush', this, this.writeBuffer);
var wbuf = this.writeBuffer;
this.writeBuffer = [];
if (!this.transport.supportsFraming) {
this.sentCallbackFn.push(this.packetsFn)
} else {
this.sentCallbackFn.push.apply(this.sentCallbackFn, this.packetsFn);
}
this.packetsFn = [];
this.transport.send(wbuf);
this.emit('drain');
this.server.emit('drain', this);
Expand Down
8 changes: 8 additions & 0 deletions lib/transports/flashsocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ FlashSocket.prototype.__proto__ = WebSocket.prototype;

FlashSocket.prototype.name = 'flashsocket';

/**
* Advertise framing support.
*
* @api public
*/

FlashSocket.prototype.supportsFraming = true;

/**
* Listens for new configuration changes of the Manager
* this way we can enable and disable the flash server.
Expand Down
8 changes: 8 additions & 0 deletions lib/transports/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ WebSocket.prototype.name = 'websocket';

WebSocket.prototype.handlesUpgrades = true;

/**
* Advertise framing support.
*
* @api public
*/

WebSocket.prototype.supportsFraming = true;

/**
* Processes the incoming data.
*
Expand Down
61 changes: 61 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,43 @@ describe('server', function () {
});
});

it('should execute in multipart packet (polling)', function (done) {
var engine = listen(function (port) {
var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] });
var i = 0;
var j = 0;

engine.on('connection', function (conn) {
conn.send('d', function (transport) {
i++;
});

conn.send('c', function (transport) {
i++;
});

conn.send('b', function (transport) {
i++;
});

conn.send('a', function (transport) {
i++;
});

});
socket.on('open', function () {
socket.on('message', function (msg) {
j++;
});
});

setTimeout(function () {
expect(i).to.be(j);
done();
}, 200);
});
});

it('should clean callback references when socket gets closed with pending callbacks', function (done) {
var engine = listen({ allowUpgrades: false }, function (port) {
var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] });
Expand All @@ -903,11 +940,35 @@ describe('server', function () {

conn.on('close', function (reason) {
expect(conn.packetsFn).to.be.empty();
expect(conn.sentCallbackFn).to.be.empty();
done();
});
});
});
});

it('should not execute when it is not actually sent (polling)', function (done) {
var engine = listen({ allowUpgrades: false }, function (port) {
var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] });

socket.transport.on('pollComplete', function(msg) {
socket.close();
});

engine.on('connection', function (conn) {
var err = undefined;

conn.send('a');
conn.send('b', function (transport) {
err = new Error('Test invalidation');
});

conn.on('close', function (reason) {
done(err);
});
});
});
});
});
});

Expand Down