Compare commits

...

5 Commits

Author SHA1 Message Date
James Coglan 19da8de414 Bump version to 0.1.2. 2011-12-05 09:39:40 +00:00
James Coglan 85ddb3f3dc Remove some comments. 2011-12-05 09:38:10 +00:00
James Coglan 7e2461e153 Fix draft-76 sockets when running behind HAProxy. 2011-12-04 21:15:24 +00:00
James Coglan ca3571f103 Don't do so many writes. 2011-12-03 21:37:38 +00:00
James Coglan 27f8f9a0f2 Detect closed sockets on the server side. 2011-12-02 10:00:25 +00:00
6 changed files with 68 additions and 37 deletions
+21
View File
@@ -0,0 +1,21 @@
defaults
mode http
timeout client 5s
timeout connect 5s
timeout server 5s
frontend all 0.0.0.0:3000
mode http
timeout client 120s
option forwardfor
option http-server-close
option http-pretend-keepalive
default_backend sockets
backend sockets
balance uri depth 2
timeout server 120s
server socket1 127.0.0.1:7000
+2 -3
View File
@@ -55,10 +55,9 @@ var WebSocket = function(request, socket, head) {
this._stream.addListener('data', function(data) {
self._parser.parse(data);
});
this._stream.addListener('close', function() {
self.close(1006, '', false);
['close', 'end', 'error'].forEach(function(event) {
self._stream.addListener(event, function() { self.close(1006, '', false) });
});
this._stream.addListener('error', function() {});
};
var API = require('./websocket/api');
+6 -9
View File
@@ -16,15 +16,12 @@ var instance = {
handshakeResponse: function() {
var stream = this._stream;
try {
stream.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n');
stream.write('Upgrade: WebSocket\r\n');
stream.write('Connection: Upgrade\r\n');
stream.write('WebSocket-Origin: ' + this._socket.request.headers.origin + '\r\n');
stream.write('WebSocket-Location: ' + this._socket.url + '\r\n\r\n');
} catch (e) {
// socket closed while writing
// no handshake sent; client will stop using WebSocket
}
stream.write( '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');
} catch (e) {}
},
parse: function(data) {
+27 -9
View File
@@ -27,6 +27,25 @@ Draft76Parser.prototype.getVersion = function() {
};
Draft76Parser.prototype.handshakeResponse = function(head) {
var request = this._socket.request,
stream = this._stream;
try {
stream.write( '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');
} catch (e) {}
this.handshakeSignature(head);
};
Draft76Parser.prototype.handshakeSignature = function(head) {
if (head.length === 0) return;
this._handshakeComplete = true;
var request = this._socket.request,
stream = this._stream,
@@ -43,16 +62,15 @@ Draft76Parser.prototype.handshakeResponse = function(head) {
MD5.update(head.toString('binary'));
try {
stream.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n', 'binary');
stream.write('Upgrade: WebSocket\r\n', 'binary');
stream.write('Connection: Upgrade\r\n', 'binary');
stream.write('Sec-WebSocket-Origin: ' + request.headers.origin + '\r\n', 'binary');
stream.write('Sec-WebSocket-Location: ' + this._socket.url + '\r\n\r\n', 'binary');
stream.write(MD5.digest('binary'), 'binary');
} catch (e) {
// socket closed while writing
// no handshake sent; client will stop using WebSocket
}
} catch (e) {};
};
Draft76Parser.prototype.parse = function(data) {
if (this._handshakeComplete)
return Draft75Parser.prototype.parse.call(this, data);
this.handshakeSignature(data);
};
module.exports = Draft76Parser;
+11 -15
View File
@@ -43,13 +43,12 @@ var Handshake = function(uri, stream) {
Handshake.prototype.requestData = function() {
var stream = this._stream, u = this._uri;
try {
stream.write('GET ' + u.pathname + (u.search || '') + ' HTTP/1.1\r\n');
stream.write('Host: ' + u.hostname + (u.port ? ':' + u.port : '') + '\r\n');
stream.write('Upgrade: websocket\r\n');
stream.write('Connection: Upgrade\r\n');
stream.write('Sec-WebSocket-Key: ' + this._key + '\r\n');
stream.write('Sec-WebSocket-Version: 8\r\n');
stream.write('\r\n');
stream.write( 'GET ' + u.pathname + (u.search || '') + ' HTTP/1.1\r\n' +
'Host: ' + u.hostname + (u.port ? ':' + u.port : '') + '\r\n' +
'Upgrade: websocket\r\n' +
'Connection: Upgrade\r\n' +
'Sec-WebSocket-Key: ' + this._key + '\r\n' +
'Sec-WebSocket-Version: 8\r\n\r\n');
} catch (e) {}
};
@@ -136,14 +135,11 @@ var instance = {
var stream = this._stream;
try {
stream.write('HTTP/1.1 101 Switching Protocols\r\n');
stream.write('Upgrade: websocket\r\n');
stream.write('Connection: Upgrade\r\n');
stream.write('Sec-WebSocket-Accept: ' + accept + '\r\n\r\n');
} catch (e) {
// socket closed while writing
// no handshake sent; client will stop using WebSocket
}
stream.write( 'HTTP/1.1 101 Switching Protocols\r\n' +
'Upgrade: websocket\r\n' +
'Connection: Upgrade\r\n' +
'Sec-WebSocket-Accept: ' + accept + '\r\n\r\n');
} catch (e) {}
},
createHandshake: function() {
+1 -1
View File
@@ -4,7 +4,7 @@
, "author" : "James Coglan <jcoglan@gmail.com> (http://jcoglan.com/)"
, "keywords" : ["websocket"]
, "version" : "0.1.1"
, "version" : "0.1.2"
, "engines" : {"node": ">=0.4.0"}
, "main" : "./lib/faye/websocket"
, "devDependencies" : {"jsclass": ">=3.0.4"}