diff --git a/lib/WebSocket.js b/lib/WebSocket.js index 70de62f37..b52d20e9b 100644 --- a/lib/WebSocket.js +++ b/lib/WebSocket.js @@ -271,6 +271,18 @@ WebSocket.prototype.terminate = function() { } }; +/** + * Expose bufferedAmount + * + * @api public + */ + +Object.defineProperty(WebSocket.prototype, 'bufferedAmount', { + get: function get() { + return this._socket ? this._socket.bufferSize : 0; + } +}); + /** * Emulates the W3C Browser based WebSocket interface using function members. * diff --git a/test/WebSocket.test.js b/test/WebSocket.test.js index 71859476c..b93f7a697 100644 --- a/test/WebSocket.test.js +++ b/test/WebSocket.test.js @@ -80,6 +80,38 @@ describe('WebSocket', function() { }); }); + describe('#bufferedAmount', function() { + it('defaults to zero', function(done) { + server.createServer(++port, function(srv) { + var url = 'ws://localhost:' + port; + var ws = new WebSocket(url); + assert.equal(0, ws.bufferedAmount); + ws.terminate(); + ws.on('close', function() { + srv.close(); + done(); + }); + }); + }); + + it('stress kernel write buffer', function(done) { + var wss = new WebSocketServer({port: ++port}, function() { + var ws = new WebSocket('ws://localhost:' + port); + }); + wss.on('connection', function(ws) { + while (true) { + if (ws.bufferedAmount > 0) break; + ws.send((new Array(10000)).join('hello')); + } + ws.terminate(); + ws.on('close', function() { + wss.close(); + done(); + }); + }); + }); + }); + describe('#readyState', function() { it('defaults to connecting', function(done) { server.createServer(++port, function(srv) {