Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support for response object on upgrade request.

  • Loading branch information...
commit 54630ee01645c95a2924902bbcd60fe4efd8dc57 1 parent bd7c52d
@stephank authored
View
4 examples/server.js
@@ -6,8 +6,8 @@ var WebSocket = require('../lib/faye/websocket'),
var port = process.argv[2] || 7000,
secure = process.argv[3] === 'ssl';
-var upgradeHandler = function(request, socket, head) {
- var ws = new WebSocket(request, socket, head, ['irc', 'xmpp'], {ping: 5});
+var upgradeHandler = function(request, response) {
+ var ws = new WebSocket(request, response, ['irc', 'xmpp'], {ping: 5});
console.log('open', ws.url, ws.version, ws.protocol);
ws.onmessage = function(event) {
View
65 lib/faye/websocket.js
@@ -31,14 +31,13 @@ var isSecureConnection = function(request) {
}
};
-var WebSocket = function(request, socket, head, supportedProtos, options) {
+var WebSocket = function(request, response, supportedProtos, options) {
+ var self = this;
+
this.request = request;
- this._stream = request.socket;
this._ping = options && options.ping;
this._pingId = 0;
-
- this._stream.setTimeout(0);
- this._stream.setNoDelay(true);
+ this._sendBuffer = [];
var scheme = isSecureConnection(request) ? 'wss:' : 'ws:';
this.url = scheme + '//' + request.headers.host + request.url;
@@ -48,32 +47,40 @@ var WebSocket = function(request, socket, head, supportedProtos, options) {
var Parser = getParser(request);
this._parser = new Parser(this, {protocols: supportedProtos});
- var self = this;
- this._sendBuffer = [];
- process.nextTick(function() { self._open() });
-
- var handshake = this._parser.handshakeResponse(head);
- try { this._stream.write(handshake, 'binary') } catch (e) {}
-
- if (this._parser.isOpen()) this.readyState = API.OPEN;
+ this._parser.writeResponseHead(response);
- if (this._ping)
- this._pingLoop = setInterval(function() {
- self._pingId += 1;
- self.ping(self._pingId.toString());
- }, this._ping * 1000);
+ this.protocol = self._parser.protocol || '';
+ this.version = self._parser.getVersion();
- this.protocol = this._parser.protocol || '';
- this.version = this._parser.getVersion();
-
- this._stream.addListener('data', function(data) {
- var response = self._parser.parse(data);
- if (!response) return;
- try { self._stream.write(response, 'binary') } catch (e) {}
- self._open();
- });
- ['close', 'end', 'error'].forEach(function(event) {
- self._stream.addListener(event, function() { self.close(1006, '', false) });
+ response.switchProtocols(function(socket, head) {
+ self._stream = socket;
+ self._stream.setTimeout(0);
+ self._stream.setNoDelay(true);
+
+ var dataListener = function(data) {
+ var reply = self._parser.parse(data);
+ if (!reply) return;
+ try { self._stream.write(reply, 'binary') } catch (e) {}
+ self._open();
+ };
+ self._stream.addListener('data', dataListener);
+
+ var closeListener = function() {
+ self.close(1006, '', false);
+ };
+ ['close', 'end', 'error'].forEach(function(event) {
+ self._stream.addListener(event, closeListener);
+ });
+
+ if (head.length !== 0) dataListener(head);
+ if (self._parser.isOpen()) self.readyState = API.OPEN;
+ process.nextTick(function() { self._open() });
+
+ if (self._ping)
+ self._pingLoop = setInterval(function() {
+ self._pingId += 1;
+ self.ping(self._pingId.toString());
+ }, self._ping * 1000);
});
};
View
14 lib/faye/websocket/draft75_parser.js
@@ -8,13 +8,13 @@ var instance = {
return 'hixie-75';
},
- handshakeResponse: function() {
- return new Buffer('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
- 'Upgrade: WebSocket\r\n' +
- 'Connection: Upgrade\r\n' +
- 'WebSocket-Origin: ' + this._socket.request.headers.origin + '\r\n' +
- 'WebSocket-Location: ' + this._socket.url + '\r\n\r\n',
- 'utf8');
+ writeResponseHead: function(response) {
+ response.writeHead(101, 'Web Socket Protocol Handshake', {
+ 'Upgrade': 'WebSocket',
+ 'Connection': 'Upgrade',
+ 'WebSocket-Origin': this._socket.request.headers.origin,
+ 'WebSocket-Location': this._socket.url
+ });
},
isOpen: function() {
View
28 lib/faye/websocket/draft76_parser.js
@@ -25,26 +25,14 @@ var bigEndian = function(number) {
Draft76Parser.prototype.getVersion = function() {
return 'hixie-76';
};
-
-Draft76Parser.prototype.handshakeResponse = function(head) {
- var request = this._socket.request, tmp;
-
- var response = new Buffer('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
- 'Upgrade: WebSocket\r\n' +
- 'Connection: Upgrade\r\n' +
- 'Sec-WebSocket-Origin: ' + request.headers.origin + '\r\n' +
- 'Sec-WebSocket-Location: ' + this._socket.url + '\r\n\r\n',
- 'binary');
-
- var signature = this.handshakeSignature(head);
- if (signature) {
- tmp = new Buffer(response.length + signature.length);
- response.copy(tmp, 0);
- signature.copy(tmp, response.length);
- response = tmp;
- }
-
- return response;
+
+Draft76Parser.prototype.writeResponseHead = function(response) {
+ response.writeHead(101, 'Web Socket Protocol Handshake', {
+ 'Upgrade': 'WebSocket',
+ 'Connection': 'Upgrade',
+ 'Sec-WebSocket-Origin': this._socket.request.headers.origin,
+ 'Sec-WebSocket-Location': this._socket.url
+ });
};
Draft76Parser.prototype.isOpen = function() {
View
19 lib/faye/websocket/hybi_parser.js
@@ -68,7 +68,7 @@ var instance = {
return 'hybi-' + version;
},
- handshakeResponse: function() {
+ writeResponseHead: function(response) {
var secKey = this._socket.request.headers['sec-websocket-key'];
if (!secKey) return null;
@@ -78,25 +78,22 @@ var instance = {
var accept = SHA1.digest('base64'),
protos = this._socket.request.headers['sec-websocket-protocol'],
supported = this._protocols,
- proto,
-
- headers = [
- 'HTTP/1.1 101 Switching Protocols',
- 'Upgrade: websocket',
- 'Connection: Upgrade',
- 'Sec-WebSocket-Accept: ' + accept
- ];
+ proto;
if (protos !== undefined && supported !== undefined) {
if (typeof protos === 'string') protos = protos.split(/\s*,\s*/);
proto = protos.filter(function(p) { return supported.indexOf(p) >= 0 })[0];
if (proto) {
this.protocol = proto;
- headers.push('Sec-WebSocket-Protocol: ' + proto);
+ response.setHeader('Sec-WebSocket-Protocol', proto);
}
}
- return new Buffer(headers.concat('','').join('\r\n'), 'utf8');
+ response.writeHead(101, {
+ 'Upgrade': 'websocket',
+ 'Connection': 'Upgrade',
+ 'Sec-WebSocket-Accept': accept
+ });
},
isOpen: function() {
View
4 spec/runner.js
@@ -15,8 +15,8 @@ EchoServer.prototype.listen = function(port, ssl) {
})
: http.createServer()
- server.addListener('upgrade', function(request, socket, head) {
- var ws = new WebSocket(request, socket, head, ["echo"])
+ server.addListener('upgrade', function(request, response) {
+ var ws = new WebSocket(request, response, ["echo"])
ws.onmessage = function(event) {
ws.send(event.data)
}
Please sign in to comment.
Something went wrong with that request. Please try again.