Compare commits

...

11 Commits

Author SHA1 Message Date
James Coglan 052120b3d5 Bump version to 0.7.1. 2013-12-03 00:49:02 +00:00
James Coglan f97fd88e02 A little formatting change around network error handling. 2013-12-02 21:44:24 +00:00
faye a8481f8cdd Merge pull request #27 from ramsperger/master
Forward networking error events to the websocket event listeners.
2013-12-02 13:31:02 -08:00
James Coglan d0c9689747 Remove unneeded options existence check. 2013-12-02 21:19:20 +00:00
James Coglan e668e64271 Expose the driver's maxLength option. 2013-12-02 21:15:32 +00:00
James Coglan f0b97a8005 Restructure tests slightly. 2013-12-02 13:08:02 +00:00
ramsperger e3cc891187 Forward networking error events to the websocket event listeners.
Handshake failures from HTTP were being fired as errors connection errors failed to show up. Dispatch network failure events (like connection refused, etc.)
2013-10-24 14:53:16 -04:00
James Coglan d1f2c2172d Bump version to 0.7.0. 2013-09-09 22:22:28 +01:00
James Coglan 0eb7d7173d Document the headers option to EventSource. 2013-08-24 11:52:47 +01:00
James Coglan 426e72786b Remove extra line break in EventSource headers. 2013-08-24 11:28:11 +01:00
James Coglan b126e31b81 Allow custom headers to be set on EventSource connections. 2013-08-24 11:21:05 +01:00
7 changed files with 68 additions and 26 deletions
+11
View File
@@ -1,3 +1,14 @@
### 0.7.1 / 2013-12-03
* Support the `maxLength` websocket-driver option
* Make the client emit `error` events on network errors
### 0.7.0 / 2013-09-09
* Allow the server to send custom headers with EventSource responses
### 0.6.1 / 2013-07-05
* Add `ca` option to the client for specifying certificate authorities
+11 -3
View File
@@ -155,6 +155,8 @@ var ws = new WebSocket.Client(url, protocols, options);
* `headers` - an object containing key-value pairs representing HTTP headers to
be sent during the handshake process
* `maxLength` - the maximum allowed size of incoming message frames, in bytes.
The default value is `2^30 - 1`, or 1 byte short of 1 GiB.
* `ping` - an integer that sets how often the WebSocket should send ping
frames, measured in seconds
@@ -243,17 +245,23 @@ The `EventSource` object exposes the following properties:
When you initialize an EventSource with ` new EventSource()`, you can pass
configuration options after the `response` parameter. Available options are:
* <b>`headers`</b> is an object containing custom headers to be set on the
EventSource response.
* <b>`retry`</b> is a number that tells the client how long (in seconds) it
should wait after a dropped connection before attempting to reconnect.
* <b>`ping`</b> is a number that tells the server how often (in seconds) to
send 'ping' packets to the client to keep the connection open, to defeat
timeouts set by proxies. The client will ignore these messages.
For example, this creates a connection that pings every 15 seconds and is
retryable every 10 seconds if the connection is broken:
For example, this creates a connection that allows access from any origin, pings
every 15 seconds and is retryable every 10 seconds if the connection is broken:
```js
var es = new EventSource(request, response, {ping: 15, retry: 10});
var es = new EventSource(request, response, {
headers: {'Access-Control-Allow-Origin': '*'},
ping: 15,
retry: 10
});
```
You can send a ping message at any time by calling `es.ping()`. Unlike
+9 -2
View File
@@ -1,6 +1,7 @@
var Stream = require('stream').Stream,
util = require('util'),
driver = require('websocket-driver'),
Headers = require('websocket-driver/lib/websocket/driver/headers'),
API = require('./websocket/api'),
EventTarget = require('./websocket/api/event_target'),
Event = require('./websocket/api/event');
@@ -18,7 +19,12 @@ var EventSource = function(request, response, options) {
this.lastEventId = request.headers['last-event-id'] || '';
this.readyState = API.CONNECTING;
var self = this;
var headers = new Headers(),
self = this;
if (options.headers) {
for (var key in options.headers) headers.set(key, options.headers[key]);
}
if (!this._stream || !this._stream.writable) return;
process.nextTick(function() { self._open() });
@@ -30,7 +36,8 @@ var EventSource = function(request, response, options) {
'Content-Type: text/event-stream\r\n' +
'Cache-Control: no-cache, no-store\r\n' +
'Connection: close\r\n' +
'\r\n\r\n' +
headers.toString() +
'\r\n' +
'retry: ' + Math.floor(this._retry * 1000) + '\r\n\r\n';
this._write(handshake);
+4 -2
View File
@@ -9,8 +9,10 @@ var util = require('util'),
API = require('./websocket/api');
var WebSocket = function(request, socket, body, protocols, options) {
this._stream = socket;
this._driver = driver.http(request, {protocols: protocols});
options = options || {};
this._stream = socket;
this._driver = driver.http(request, {maxLength: options.maxLength, protocols: protocols});
var self = this;
if (!this._stream || !this._stream.writable) return;
+14 -6
View File
@@ -2,12 +2,15 @@ var util = require('util'),
net = require('net'),
tls = require('tls'),
driver = require('websocket-driver'),
API = require('./api');
API = require('./api'),
Event = require('./api/event');
var Client = function(url, protocols, options) {
options = options || {};
this.url = url;
this._uri = require('url').parse(url);
this._driver = driver.client(url, {protocols: protocols});
this._driver = driver.client(url, {maxLength: options.maxLength, protocols: protocols});
['open', 'error'].forEach(function(event) {
this._driver.on(event, function() {
@@ -21,7 +24,7 @@ var Client = function(url, protocols, options) {
tlsOptions = {},
self = this;
if (options && options.ca) tlsOptions.ca = options.ca;
if (options.ca) tlsOptions.ca = options.ca;
var connection = secure
? tls.connect(this._uri.port || 443, this._uri.hostname, tlsOptions, onConnect)
@@ -35,9 +38,14 @@ var Client = function(url, protocols, options) {
API.call(this, options);
['error', 'end'].forEach(function(event) {
this._stream.on(event, function() { self._finalize('', 1006) });
}, this);
this._stream.on('end', function() { self._finalize('', 1006) });
this._stream.on('error', function(error) {
var event = new Event('error', {message: 'Network error: ' + url + ': ' + error.message});
event.initEvent('error', false, false);
self.dispatchEvent(event);
self._finalize('', 1006);
});
};
util.inherits(Client, API);
+2 -2
View File
@@ -5,10 +5,10 @@
, "keywords" : ["websocket", "eventsource"]
, "license" : "MIT"
, "version" : "0.6.1"
, "version" : "0.7.1"
, "engines" : {"node": ">=0.4.0"}
, "main" : "./lib/faye/websocket"
, "dependencies" : {"websocket-driver": ">=0.2.0"}
, "dependencies" : {"websocket-driver": ">=0.3.1"}
, "devDependencies" : {"jstest": "", "pace": ""}
, "scripts" : {"test": "jstest spec/runner.js"}
+17 -11
View File
@@ -7,12 +7,12 @@ var WebSocketSteps = test.asyncSteps({
this._adapter = new EchoServer()
this._adapter.listen(port, secure)
this._port = port
setTimeout(callback, 100)
process.nextTick(callback)
},
stop: function(callback) {
this._adapter.stop()
setTimeout(callback, 100)
process.nextTick(callback)
},
open_socket: function(url, protocols, callback) {
@@ -59,14 +59,20 @@ var WebSocketSteps = test.asyncSteps({
},
listen_for_message: function(callback) {
var self = this
var time = new Date().getTime(), self = this
this._ws.addEventListener('message', function(message) { self._message = message.data })
callback()
var timer = setInterval(function() {
if (self._message || new Date().getTime() - time > 3000) {
clearInterval(timer)
callback()
}
}, 100)
},
send_message: function(message, callback) {
this._ws.send(message)
setTimeout(callback, 100)
var ws = this._ws
setTimeout(function() { ws.send(message) }, 500)
process.nextTick(callback)
},
check_response: function(message, callback) {
@@ -110,26 +116,26 @@ test.describe("Client", function() { with(this) {
}})
it("can send and receive messages", function() { with(this) {
listen_for_message()
send_message("I expect this to be echoed")
listen_for_message()
check_response("I expect this to be echoed")
}})
it("sends numbers as strings", function() { with(this) {
listen_for_message()
send_message(13)
listen_for_message()
check_response("13")
}})
it("sends booleans as strings", function() { with(this) {
listen_for_message()
send_message(false)
listen_for_message()
check_response("false")
}})
it("sends arrays as strings", function() { with(this) {
listen_for_message()
send_message([13,14,15])
listen_for_message()
check_response("13,14,15")
}})
}})
@@ -141,8 +147,8 @@ test.describe("Client", function() { with(this) {
}})
it("cannot send and receive messages", function() { with(this) {
listen_for_message()
send_message("I expect this to be echoed")
listen_for_message()
check_no_response()
}})
}})