From 834cbcf7a58c4b99ef9d521302e2b14605059c08 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Fri, 10 Aug 2012 10:39:00 -0700 Subject: [PATCH 1/4] README: added flush/drain docs --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 173765d6..2a26970b 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,18 @@ For more information on the client refer to the These are exposed by `require('engine.io')`: +##### Events + +- `flush` + - Called when a socket buffer is being flushed. + - **Arguments** + - `Socket`: socket being flushed + - `Array`: write buffer +- `drain` + - Called when a socket buffer is drained + - **Arguments** + - `Socket`: socket being flushed + ##### Properties - `protocol` _(Number)_: protocol revision number @@ -140,7 +152,7 @@ The main server/manager. _Inherits from EventEmitter_. ##### Events - `connection` - - Fired when a new connection is established. + - Fired when a new connection is established. - **Arguments** - `Socket`: a Socket object @@ -215,6 +227,12 @@ A representation of a client. _Inherits from EventEmitter_. - Fired when an error occurs. - **Arguments** - `Error`: error object +- `flush` + - Called when the write buffer is being flushed. + - **Arguments** + - `Array`: write buffer +- `drain` + - Called when the write buffer is drained ##### Properties From 271585189f757ea9eb9344aa3af828cfc7735758 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Fri, 10 Aug 2012 10:39:09 -0700 Subject: [PATCH 2/4] socket: added flush/drain events (fixes #56) --- lib/socket.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/socket.js b/lib/socket.js index 526c1fe4..2d1f0286 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -252,8 +252,12 @@ Socket.prototype.flush = function () { if ('closed' != this.readyState && this.transport.writable && this.writeBuffer.length) { debug('flushing buffer to transport'); + this.emit('flush', this); + this.server.emit('flush', this, this.writeBuffer); this.transport.send(this.writeBuffer); this.writeBuffer = []; + this.emit('drain'); + this.server.emit('drain', this); } }; @@ -273,7 +277,7 @@ Socket.prototype.getAvailableUpgrades = function () { } } return availableUpgrades; -} +}; /** * Closes the socket and underlying transport. From b948f858fbf9fedfd699de6ced1f59810d4cf8f2 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Fri, 10 Aug 2012 10:45:48 -0700 Subject: [PATCH 3/4] socket: fix `flush` event parameter --- lib/socket.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/socket.js b/lib/socket.js index 2d1f0286..78db819e 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -252,7 +252,7 @@ Socket.prototype.flush = function () { if ('closed' != this.readyState && this.transport.writable && this.writeBuffer.length) { debug('flushing buffer to transport'); - this.emit('flush', this); + this.emit('flush', this.writeBuffer); this.server.emit('flush', this, this.writeBuffer); this.transport.send(this.writeBuffer); this.writeBuffer = []; From 81a6e76426f9bc2ad891a2c0956c98b6a0c2884b Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Fri, 10 Aug 2012 10:46:00 -0700 Subject: [PATCH 4/4] test: added flush/drain events tests --- test/server.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/server.js b/test/server.js index 5d75983f..743cc35a 100644 --- a/test/server.js +++ b/test/server.js @@ -558,6 +558,38 @@ describe('server', function () { }); }); }); + + it('should trigger a flush/drain event', function(done){ + var engine = listen({ allowUpgrades: false }, function(port){ + engine.on('connection', function(socket){ + var totalEvents = 4; + + engine.on('flush', function(sock, buf){ + expect(sock).to.be(socket); + expect(buf).to.be.an('array'); + --totalEvents || done(); + }); + socket.on('flush', function(buf){ + expect(buf).to.be.an('array'); + --totalEvents || done(); + }); + + engine.on('drain', function(sock){ + expect(sock).to.be(socket); + expect(socket.writeBuffer.length).to.be(0); + --totalEvents || done(); + }); + socket.on('drain', function(){ + expect(socket.writeBuffer.length).to.be(0); + --totalEvents || done(); + }); + + socket.send('aaaa'); + }); + + new eioc.Socket('ws://localhost:%d'.s(port)); + }); + }); }); describe('upgrade', function () {