Skip to content

Commit a103bd5

Browse files
committed
Merge pull request #132 from roamm/send_packet_callback
Fixed bugs in the previous send callback fix and added new test cases
2 parents ead08d6 + 148f654 commit a103bd5

File tree

4 files changed

+96
-5
lines changed

4 files changed

+96
-5
lines changed

lib/socket.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function Socket (id, server, transport) {
2424
this.readyState = 'opening';
2525
this.writeBuffer = [];
2626
this.packetsFn = [];
27+
this.sentCallbackFn = [];
2728

2829
this.setTransport(transport);
2930
this.onOpen();
@@ -226,6 +227,7 @@ Socket.prototype.clearTransport = function () {
226227
Socket.prototype.onClose = function (reason, description) {
227228
if ('closed' != this.readyState) {
228229
this.packetsFn = [];
230+
this.sentCallbackFn = [];
229231
this.clearTransport();
230232
this.readyState = 'closed';
231233
this.emit('close', reason, description);
@@ -242,11 +244,18 @@ Socket.prototype.setupSendCallback = function () {
242244
var self = this;
243245
//the message was sent successfully, execute the callback
244246
this.transport.on('drain', function() {
245-
if (self.packetsFn.length > 0) {
246-
var seqFn = self.packetsFn.splice(0,1)[0];
247+
if (self.sentCallbackFn.length > 0) {
248+
var seqFn = self.sentCallbackFn.splice(0,1)[0];
247249
if ('function' == typeof seqFn) {
248250
debug('executing send callback');
249251
seqFn(self.transport);
252+
} else if (Array.isArray(seqFn)) {
253+
debug('executing batch send callback');
254+
for (var i in seqFn) {
255+
if ('function' == typeof seqFn[i]) {
256+
seqFn[i](self.transport);
257+
}
258+
}
250259
}
251260
}
252261
});
@@ -288,9 +297,8 @@ Socket.prototype.sendPacket = function (type, data, callback) {
288297
this.writeBuffer.push(packet);
289298

290299
//add send callback to object
291-
if (callback) {
292-
this.packetsFn.push(callback);
293-
}
300+
this.packetsFn.push(callback);
301+
294302
this.flush();
295303
}
296304
};
@@ -309,6 +317,12 @@ Socket.prototype.flush = function () {
309317
this.server.emit('flush', this, this.writeBuffer);
310318
var wbuf = this.writeBuffer;
311319
this.writeBuffer = [];
320+
if (!this.transport.supportsFraming) {
321+
this.sentCallbackFn.push(this.packetsFn)
322+
} else {
323+
this.sentCallbackFn.push.apply(this.sentCallbackFn, this.packetsFn);
324+
}
325+
this.packetsFn = [];
312326
this.transport.send(wbuf);
313327
this.emit('drain');
314328
this.server.emit('drain', this);

lib/transports/flashsocket.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ FlashSocket.prototype.__proto__ = WebSocket.prototype;
3636

3737
FlashSocket.prototype.name = 'flashsocket';
3838

39+
/**
40+
* Advertise framing support.
41+
*
42+
* @api public
43+
*/
44+
45+
FlashSocket.prototype.supportsFraming = true;
46+
3947
/**
4048
* Listens for new configuration changes of the Manager
4149
* this way we can enable and disable the flash server.

lib/transports/websocket.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ WebSocket.prototype.name = 'websocket';
5555

5656
WebSocket.prototype.handlesUpgrades = true;
5757

58+
/**
59+
* Advertise framing support.
60+
*
61+
* @api public
62+
*/
63+
64+
WebSocket.prototype.supportsFraming = true;
65+
5866
/**
5967
* Processes the incoming data.
6068
*

test/server.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,43 @@ describe('server', function () {
883883
});
884884
});
885885

886+
it('should execute in multipart packet (polling)', function (done) {
887+
var engine = listen(function (port) {
888+
var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] });
889+
var i = 0;
890+
var j = 0;
891+
892+
engine.on('connection', function (conn) {
893+
conn.send('d', function (transport) {
894+
i++;
895+
});
896+
897+
conn.send('c', function (transport) {
898+
i++;
899+
});
900+
901+
conn.send('b', function (transport) {
902+
i++;
903+
});
904+
905+
conn.send('a', function (transport) {
906+
i++;
907+
});
908+
909+
});
910+
socket.on('open', function () {
911+
socket.on('message', function (msg) {
912+
j++;
913+
});
914+
});
915+
916+
setTimeout(function () {
917+
expect(i).to.be(j);
918+
done();
919+
}, 200);
920+
});
921+
});
922+
886923
it('should clean callback references when socket gets closed with pending callbacks', function (done) {
887924
var engine = listen({ allowUpgrades: false }, function (port) {
888925
var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] });
@@ -903,11 +940,35 @@ describe('server', function () {
903940

904941
conn.on('close', function (reason) {
905942
expect(conn.packetsFn).to.be.empty();
943+
expect(conn.sentCallbackFn).to.be.empty();
906944
done();
907945
});
908946
});
909947
});
910948
});
949+
950+
it('should not execute when it is not actually sent (polling)', function (done) {
951+
var engine = listen({ allowUpgrades: false }, function (port) {
952+
var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] });
953+
954+
socket.transport.on('pollComplete', function(msg) {
955+
socket.close();
956+
});
957+
958+
engine.on('connection', function (conn) {
959+
var err = undefined;
960+
961+
conn.send('a');
962+
conn.send('b', function (transport) {
963+
err = new Error('Test invalidation');
964+
});
965+
966+
conn.on('close', function (reason) {
967+
done(err);
968+
});
969+
});
970+
});
971+
});
911972
});
912973
});
913974

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy