From afa76b4f238e0e80b162b7a0bbc5f2ff7e0ff700 Mon Sep 17 00:00:00 2001 From: Gabe Hollombe Date: Mon, 23 Apr 2012 14:00:06 +0700 Subject: [PATCH 1/3] add auto flush property (defaults to false) and a flushBuffer method to allow manual buffer flushing --- lib/socket.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/socket.js b/lib/socket.js index 0af8a241..7e08d8c4 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -36,6 +36,7 @@ , 'sync disconnect on unload': true , 'auto connect': true , 'flash policy port': 10843 + , 'auto flush': true }; io.util.merge(this.options, options); @@ -305,11 +306,25 @@ this.doBuffer = v; if (!v && this.connected && this.buffer.length) { - this.transport.payload(this.buffer); - this.buffer = []; + if (this.options['auto flush']) { + this.flushBuffer(); + } } }; + /** + * Flushes the buffer data over the wire. + * To be invoked manually when 'auto flush' is set to false. + * + * @api public + */ + + Socket.prototype.flushBuffer = function() { + this.transport.payload(this.buffer); + this.buffer = []; + }; + + /** * Disconnect the established connect. * From 76cf0b30cf43fd11786480988c1c9904da5e8353 Mon Sep 17 00:00:00 2001 From: Gabe Hollombe Date: Mon, 23 Apr 2012 18:01:37 +0700 Subject: [PATCH 2/3] added tests for auto and manual buffer flushing --- support/test-runner/app.js | 16 ++++++++++++++ test/socket.test.js | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/support/test-runner/app.js b/support/test-runner/app.js index 17e78fbe..983cae94 100644 --- a/support/test-runner/app.js +++ b/support/test-runner/app.js @@ -158,6 +158,22 @@ suite('socket.test.js', function () { }); }); + server('test manual buffer flushing', function (io) { + io.sockets.on('connection', function (socket) { + socket.on('message', function (msg) { + socket.send(msg); + }); + }); + }); + + server('test automatic buffer flushing', function (io) { + io.sockets.on('connection', function (socket) { + socket.on('message', function (msg) { + socket.send(msg); + }); + }); + }); + server('test acks sent from client', function (io) { io.sockets.on('connection', function (socket) { socket.send('tobi', function () { diff --git a/test/socket.test.js b/test/socket.test.js index d1b319e8..3951de2d 100644 --- a/test/socket.test.js +++ b/test/socket.test.js @@ -69,6 +69,49 @@ }); }, + 'test manual buffer flushing': function (next) { + var socket = create(); + + socket.socket.options['auto flush'] = false; + + socket.on('error', function (msg) { + throw new Error(msg || 'Received an error'); + }); + + socket.on('connect', function () { + socket.socket.connected = false; + socket.send('buffered'); + socket.socket.onConnect(); + socket.socket.flushBuffer(); + + socket.on('message', function (msg) { + msg.should().equal('buffered'); + socket.disconnect(); + next(); + }); + }); + }, + + 'test automatic buffer flushing': function (next) { + var socket = create(); + + socket.on('error', function (msg) { + throw new Error(msg || 'Received an error'); + }); + + socket.on('connect', function () { + socket.socket.connected = false; + socket.send('buffered'); + socket.socket.onConnect(); + + socket.on('message', function (msg) { + msg.should().equal('buffered'); + socket.disconnect(); + next(); + }); + }); + }, + 'test acks sent from client': function (next) { var socket = create(); From a55ed9bf574ac701e0aba09c4cee6d52b1fa6784 Mon Sep 17 00:00:00 2001 From: Chelsea Robb Date: Tue, 24 Apr 2012 12:52:23 +1000 Subject: [PATCH 3/3] Renamed 'auto flush' option to manualFlush --- lib/socket.js | 6 +++--- test/socket.test.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/socket.js b/lib/socket.js index 7e08d8c4..1dba954b 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -36,7 +36,7 @@ , 'sync disconnect on unload': true , 'auto connect': true , 'flash policy port': 10843 - , 'auto flush': true + , 'manualFlush': false }; io.util.merge(this.options, options); @@ -306,7 +306,7 @@ this.doBuffer = v; if (!v && this.connected && this.buffer.length) { - if (this.options['auto flush']) { + if (!this.options['manualFlush']) { this.flushBuffer(); } } @@ -314,7 +314,7 @@ /** * Flushes the buffer data over the wire. - * To be invoked manually when 'auto flush' is set to false. + * To be invoked manually when 'manualFlush' is set to true. * * @api public */ diff --git a/test/socket.test.js b/test/socket.test.js index 3951de2d..eae49564 100644 --- a/test/socket.test.js +++ b/test/socket.test.js @@ -72,7 +72,7 @@ 'test manual buffer flushing': function (next) { var socket = create(); - socket.socket.options['auto flush'] = false; + socket.socket.options['manualFlush'] = true; socket.on('error', function (msg) { throw new Error(msg || 'Received an error');