Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b861f1a779 | |||
| 047f87e4cd | |||
| d6728e115d | |||
| 15aedb7ca3 | |||
| 7e9149faff |
@@ -1,7 +1,6 @@
|
||||
.git
|
||||
.gitignore
|
||||
.npmignore
|
||||
.redcar
|
||||
.travis.yml
|
||||
node_modules
|
||||
spec
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
### 0.6.0 / 2013-05-12
|
||||
|
||||
* Add support for custom headers
|
||||
|
||||
|
||||
### 0.5.0 / 2013-05-05
|
||||
|
||||
* Extract the protocol handlers into the `websocket-driver` library
|
||||
|
||||
@@ -52,13 +52,19 @@ server.on('upgrade', function(request, socket, body) {
|
||||
console.log('close', event.code, event.reason);
|
||||
ws = null;
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(8000);
|
||||
```
|
||||
|
||||
`WebSocket` objects are also duplex streams, so you could replace the
|
||||
`ws.on('message', ...)` line with:
|
||||
|
||||
```js
|
||||
ws.pipe(ws);
|
||||
```
|
||||
|
||||
Note that under certain circumstances (notably a draft-76 client connecting
|
||||
through an HTTP proxy), the WebSocket handshake will not be complete after you
|
||||
call `new WebSocket()` because the server will not have received the entire
|
||||
@@ -108,6 +114,9 @@ ws.on('close', function(event) {
|
||||
});
|
||||
```
|
||||
|
||||
The WebSocket client also lets you inspect the status and headers of the
|
||||
handshake response via its `statusCode` and `headers` properties.
|
||||
|
||||
|
||||
## Subprotocol negotiation
|
||||
|
||||
@@ -131,11 +140,28 @@ If the client and server agree on a protocol, both the client- and server-side
|
||||
socket objects expose the selected protocol through the `ws.protocol` property.
|
||||
|
||||
|
||||
## Initialization options
|
||||
|
||||
Both the server- and client-side classes allow an options object to be passed
|
||||
in at initialization time, for example:
|
||||
|
||||
```js
|
||||
var ws = new WebSocket(request, socket, body, protocols, options);
|
||||
var ws = new WebSocket.Client(url, protocols, options);
|
||||
```
|
||||
|
||||
`protocols` is an array of subprotocols as described above, or `null`.
|
||||
`options` is an optional object containing any of these fields:
|
||||
|
||||
* `headers` - an object containing key-value pairs representing HTTP headers to
|
||||
be sent during the handshake process
|
||||
* `ping` - an integer that sets how often the WebSocket should send ping
|
||||
frames, measured in seconds
|
||||
|
||||
|
||||
## WebSocket API
|
||||
|
||||
Both server- and client-side `WebSocket` objects support the following API.
|
||||
They are both readable and writable streams, so for example you can write an
|
||||
echo server using `socket.pipe(socket)`.
|
||||
|
||||
* <b>`on('open', function(event) {})`</b> fires when the socket connection is
|
||||
established. Event has no attributes.
|
||||
|
||||
+3
-1
@@ -2,7 +2,9 @@ var WebSocket = require('../lib/faye/websocket'),
|
||||
port = process.argv[2] || 7000,
|
||||
secure = process.argv[3] === 'ssl',
|
||||
scheme = secure ? 'wss' : 'ws',
|
||||
ws = new WebSocket.Client(scheme + '://localhost:' + port + '/');
|
||||
url = scheme + '://localhost:' + port + '/',
|
||||
headers = {Origin: 'http://faye.jcoglan.com'},
|
||||
ws = new WebSocket.Client(url, null, {headers: headers});
|
||||
|
||||
console.log('Connecting to ' + ws.url);
|
||||
|
||||
|
||||
@@ -10,8 +10,6 @@ var util = require('util'),
|
||||
|
||||
var WebSocket = function(request, socket, body, protocols, options) {
|
||||
this._stream = socket;
|
||||
this._ping = options && options.ping;
|
||||
this._pingId = 0;
|
||||
this._driver = driver.http(request, {protocols: protocols});
|
||||
|
||||
var self = this;
|
||||
@@ -24,7 +22,7 @@ var WebSocket = function(request, socket, body, protocols, options) {
|
||||
this._stream.setNoDelay(true);
|
||||
|
||||
this._driver.io.write(body);
|
||||
API.call(this);
|
||||
API.call(this, options);
|
||||
|
||||
['error', 'end'].forEach(function(event) {
|
||||
this._stream.on(event, function() { self._finalize('', 1006) });
|
||||
|
||||
@@ -3,9 +3,18 @@ var Stream = require('stream').Stream,
|
||||
EventTarget = require('./api/event_target'),
|
||||
Event = require('./api/event');
|
||||
|
||||
var API = function() {
|
||||
var API = function(options) {
|
||||
options = options || {};
|
||||
|
||||
this.readable = this.writable = true;
|
||||
|
||||
var headers = options.headers;
|
||||
if (headers) {
|
||||
for (var name in headers) this._driver.setHeader(name, headers[name]);
|
||||
}
|
||||
|
||||
this._ping = options.ping;
|
||||
this._pingId = 0;
|
||||
this.readyState = API.CONNECTING;
|
||||
this.bufferedAmount = 0;
|
||||
this.protocol = '';
|
||||
|
||||
@@ -7,10 +7,15 @@ var util = require('util'),
|
||||
var Client = function(url, protocols, options) {
|
||||
this.url = url;
|
||||
this._uri = require('url').parse(url);
|
||||
this._ping = options && options.ping;
|
||||
this._pingId = 0;
|
||||
this._driver = driver.client(url, {protocols: protocols});
|
||||
|
||||
['open', 'error'].forEach(function(event) {
|
||||
this._driver.on(event, function() {
|
||||
self.headers = self._driver.headers;
|
||||
self.statusCode = self._driver.statusCode;
|
||||
});
|
||||
}, this);
|
||||
|
||||
var secure = (this._uri.protocol === 'wss:'),
|
||||
onConnect = function() { self._driver.start() },
|
||||
tlsOptions = {},
|
||||
@@ -28,7 +33,7 @@ var Client = function(url, protocols, options) {
|
||||
|
||||
if (!secure) this._stream.on('connect', onConnect);
|
||||
|
||||
API.call(this);
|
||||
API.call(this, options);
|
||||
|
||||
['error', 'end'].forEach(function(event) {
|
||||
this._stream.on(event, function() { self._finalize('', 1006) });
|
||||
|
||||
+2
-2
@@ -4,10 +4,10 @@
|
||||
, "author" : "James Coglan <jcoglan@gmail.com> (http://jcoglan.com/)"
|
||||
, "keywords" : ["websocket", "eventsource"]
|
||||
|
||||
, "version" : "0.5.0"
|
||||
, "version" : "0.6.0"
|
||||
, "engines" : {"node": ">=0.4.0"}
|
||||
, "main" : "./lib/faye/websocket"
|
||||
, "dependencies" : {"websocket-driver": ""}
|
||||
, "dependencies" : {"websocket-driver": ">=0.2.0"}
|
||||
, "devDependencies" : {"jsclass": "", "pace": ""}
|
||||
|
||||
, "scripts" : {"test": "node spec/runner.js"}
|
||||
|
||||
Reference in New Issue
Block a user