Comparing version 0.4.29 to 0.4.30
/// shim for browser packaging | ||
module.exports = function() { | ||
return global.WebSocket || global.MozWebSocket; | ||
} | ||
module.exports = global.WebSocket || global.MozWebSocket; |
@@ -42,5 +42,17 @@ /*! | ||
function WebSocket(address, options) { | ||
var self = this; | ||
function WebSocket(address, protocols, options) { | ||
if (protocols && !Array.isArray(protocols) && 'object' == typeof protocols) { | ||
// accept the "options" Object as the 2nd argument | ||
options = protocols; | ||
protocols = null; | ||
} | ||
if ('string' == typeof protocols) { | ||
protocols = [ protocols ]; | ||
} | ||
if (!Array.isArray(protocols)) { | ||
protocols = []; | ||
} | ||
// TODO: actually handle the `Sub-Protocols` part of the WebSocket client | ||
this._socket = null; | ||
@@ -51,6 +63,7 @@ this.bytesReceived = 0; | ||
if (Object.prototype.toString.call(address) == '[object Array]') { | ||
if (Array.isArray(address)) { | ||
initAsServerClient.apply(this, address.concat(options)); | ||
} else { | ||
initAsClient.apply(this, [address, protocols, options]); | ||
} | ||
else initAsClient.apply(this, arguments); | ||
} | ||
@@ -223,2 +236,3 @@ | ||
} | ||
var self = this; | ||
if (typeof cb != 'function') throw new Error('callback must be provided'); | ||
@@ -231,3 +245,2 @@ if (this.readyState != WebSocket.OPEN) { | ||
if (this._queue) { | ||
var self = this; | ||
this._queue.push(function() { self.stream(options, cb); }); | ||
@@ -239,3 +252,2 @@ return; | ||
startQueue(this); | ||
var self = this; | ||
var send = function(data, final) { | ||
@@ -297,3 +309,7 @@ try { | ||
get: function get() { | ||
return this._socket ? this._socket.bufferSize : 0; | ||
var amount = 0; | ||
if (this._socket) { | ||
amount = this._socket.bufferSize || 0; | ||
} | ||
return amount; | ||
} | ||
@@ -447,3 +463,3 @@ }); | ||
function initAsClient(address, options) { | ||
function initAsClient(address, protocols, options) { | ||
options = new Options({ | ||
@@ -450,0 +466,0 @@ origin: null, |
@@ -5,3 +5,3 @@ { | ||
"description": "simple to use, blazing fast and thoroughly tested websocket client, server and console for node.js, up-to-date against RFC-6455", | ||
"version": "0.4.29", | ||
"version": "0.4.30", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "Hixie", |
@@ -66,2 +66,14 @@ [![Build Status](https://secure.travis-ci.org/einaros/ws.png)](http://travis-ci.org/einaros/ws) | ||
### Server sending broadcast data ### | ||
```js | ||
var WebSocketServer = require('ws').Server | ||
, wss = new WebSocketServer({port: 8080}); | ||
wss.broadcast = function(data) { | ||
for(var i in this.clients) | ||
this.clients[i].send(data); | ||
}; | ||
``` | ||
### Error handling best practices ### | ||
@@ -68,0 +80,0 @@ |
@@ -56,2 +56,14 @@ var assert = require('assert') | ||
}); | ||
// GH-227 | ||
it('should accept the `options` object as the 3rd argument', function(done) { | ||
var wss = new WebSocketServer({port: ++port}, function() { | ||
var agent = { | ||
addRequest: function() { | ||
wss.close(); | ||
done(); | ||
} | ||
}; | ||
var ws = new WebSocket('ws://localhost:' + port, [], { agent: agent }); | ||
}); | ||
}); | ||
}); | ||
@@ -114,2 +126,17 @@ | ||
it('defaults to zero upon "open"', function(done) { | ||
server.createServer(++port, function(srv) { | ||
var url = 'ws://localhost:' + port; | ||
var ws = new WebSocket(url); | ||
ws.onopen = function() { | ||
assert.equal(0, ws.bufferedAmount); | ||
ws.terminate(); | ||
ws.on('close', function() { | ||
srv.close(); | ||
done(); | ||
}); | ||
}; | ||
}); | ||
}); | ||
it('stress kernel write buffer', function(done) { | ||
@@ -1515,3 +1542,3 @@ var wss = new WebSocketServer({port: ++port}, function() { | ||
it('can connect to secure websocket server with client side certificate', function(done) { | ||
it('can connect to secure websocket server with client side certificate', function(done) { | ||
var options = { | ||
@@ -1675,28 +1702,27 @@ key: fs.readFileSync('test/fixtures/key.pem'), | ||
describe('host and origin headers', function() { | ||
it('includes the host header with port number', function(done) { | ||
var srv = http.createServer(); | ||
srv.listen(++port, function(){ | ||
srv.on('upgrade', function(req, socket, upgradeHeade) { | ||
assert.equal('localhost:' + port, req.headers['host']); | ||
srv.close(); | ||
done(); | ||
}); | ||
var ws = new WebSocket('ws://localhost:' + port); | ||
}); | ||
}); | ||
it('includes the host header with port number', function(done) { | ||
var srv = http.createServer(); | ||
srv.listen(++port, function(){ | ||
srv.on('upgrade', function(req, socket, upgradeHeade) { | ||
assert.equal('localhost:' + port, req.headers['host']); | ||
srv.close(); | ||
done(); | ||
}); | ||
var ws = new WebSocket('ws://localhost:' + port); | ||
}); | ||
}); | ||
it('includes the origin header with port number', function(done) { | ||
var srv = http.createServer(); | ||
srv.listen(++port, function() { | ||
srv.on('upgrade', function(req, socket, upgradeHeade) { | ||
assert.equal('localhost:' + port, req.headers['origin']); | ||
srv.close(); | ||
done(); | ||
}); | ||
var ws = new WebSocket('ws://localhost:' + port); | ||
}); | ||
}); | ||
it('includes the origin header with port number', function(done) { | ||
var srv = http.createServer(); | ||
srv.listen(++port, function() { | ||
srv.on('upgrade', function(req, socket, upgradeHeade) { | ||
assert.equal('localhost:' + port, req.headers['origin']); | ||
srv.close(); | ||
done(); | ||
}); | ||
var ws = new WebSocket('ws://localhost:' + port); | ||
}); | ||
}); | ||
}); | ||
}); |
262733
64
6529
172