From 0dfa68c710412ec06a14abbd5a80c4c8fab0c84f Mon Sep 17 00:00:00 2001 From: roam Date: Thu, 20 Dec 2012 00:49:32 +0800 Subject: [PATCH] Fixed packet send callback design issue There were two issues here. 1. When Socket.send called with or without callback alternately, the trigger order is incorrect. 2. The 'drain' event from transport is one per packet for transports supporting framing like websocket and is all in one for those without framing like polling. --- lib/socket.js | 15 ++++++++++++--- lib/transports/flashsocket.js | 8 ++++++++ lib/transports/websocket.js | 8 ++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/socket.js b/lib/socket.js index 5a2f0b91b..81ffbb316 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -247,6 +247,13 @@ Socket.prototype.setupSendCallback = function () { 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) { + seqFn[i](self.transport); + } + } } } }); @@ -288,9 +295,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(); } }; @@ -309,6 +315,9 @@ Socket.prototype.flush = function () { this.server.emit('flush', this, this.writeBuffer); var wbuf = this.writeBuffer; this.writeBuffer = []; + if (!this.transport.supportsFraming) { + this.packetsFn = [this.packetsFn]; + } this.transport.send(wbuf); this.emit('drain'); this.server.emit('drain', this); diff --git a/lib/transports/flashsocket.js b/lib/transports/flashsocket.js index 4773c1114..794bb29fa 100644 --- a/lib/transports/flashsocket.js +++ b/lib/transports/flashsocket.js @@ -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. diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 6c474c8e6..448699e5e 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -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. *