diff --git a/lib/socket.js b/lib/socket.js index 6132ada2..8491c678 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 + , 'manualFlush': false }; io.util.merge(this.options, options); @@ -311,11 +312,25 @@ this.doBuffer = v; if (!v && this.connected && this.buffer.length) { - this.transport.payload(this.buffer); - this.buffer = []; + if (!this.options['manualFlush']) { + this.flushBuffer(); + } } }; + /** + * Flushes the buffer data over the wire. + * To be invoked manually when 'manualFlush' is set to true. + * + * @api public + */ + + Socket.prototype.flushBuffer = function() { + this.transport.payload(this.buffer); + this.buffer = []; + }; + + /** * Disconnect the established connect. * 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..eae49564 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['manualFlush'] = true; + + 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();