faye-websocket
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -0,1 +1,8 @@ | ||
=== 0.4.0 / 2012-02-13 | ||
* Add ping() method to server-side WebSocket and EventSource | ||
* Buffer send() calls until the draft-76 handshake is complete | ||
* Fix HTTPS problems on Node 0.7 | ||
=== 0.3.1 / 2012-01-16 | ||
@@ -2,0 +9,0 @@ |
@@ -10,3 +10,3 @@ var WebSocket = require('../lib/faye/websocket'), | ||
var upgradeHandler = function(request, socket, head) { | ||
var ws = new WebSocket(request, socket, head, ['irc', 'xmpp']); | ||
var ws = new WebSocket(request, socket, head, ['irc', 'xmpp'], {ping: 5}); | ||
console.log('open', ws.url, ws.version, ws.protocol); | ||
@@ -13,0 +13,0 @@ |
@@ -29,17 +29,8 @@ var API = require('./websocket/api'), | ||
this.lastEventId = request.headers['last-event-id'] || ''; | ||
this.readyState = API.OPEN; | ||
var event = new Event('open'); | ||
event.initEvent('open', false, false); | ||
this.dispatchEvent(event); | ||
var self = this; | ||
this._pingLoop = setInterval(function() { | ||
try { this._stream.write(':\r\n\r\n') } catch (e) {} | ||
}, this._ping * 1000); | ||
this.readyState = API.CONNECTING; | ||
this._sendBuffer = []; | ||
process.nextTick(function() { self._open() }); | ||
['close', 'end', 'error'].forEach(function(event) { | ||
self._stream.addListener(event, function() { self.close() }); | ||
}); | ||
var handshake = 'HTTP/1.1 200 OK\r\n' + | ||
@@ -54,2 +45,11 @@ 'Content-Type: text/event-stream\r\n' + | ||
} catch (e) {} | ||
this.readyState = API.OPEN; | ||
if (this._ping) | ||
this._pingLoop = setInterval(function() { self.ping() }, this._ping * 1000); | ||
['close', 'end', 'error'].forEach(function(event) { | ||
self._stream.addListener(event, function() { self.close() }); | ||
}); | ||
}; | ||
@@ -80,2 +80,11 @@ | ||
ping: function() { | ||
try { | ||
this._stream.write(':\r\n\r\n', 'utf8'); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
}, | ||
close: function() { | ||
@@ -82,0 +91,0 @@ if (this.readyState === API.CLOSING || this.readyState === API.CLOSED) |
@@ -34,5 +34,7 @@ // API and protocol references: | ||
var WebSocket = function(request, socket, head, supportedProtos) { | ||
var WebSocket = function(request, socket, head, supportedProtos, options) { | ||
this.request = request; | ||
this._stream = request.socket; | ||
this._ping = options && options.ping; | ||
this._pingId = 0; | ||
@@ -50,15 +52,20 @@ this._stream.setTimeout(0); | ||
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; | ||
if (this._ping) | ||
this._pingLoop = setInterval(function() { | ||
self._pingId += 1; | ||
self.ping(self._pingId.toString()); | ||
}, this._ping * 1000); | ||
this.protocol = this._parser.protocol || ''; | ||
this.readyState = API.OPEN; | ||
this.version = this._parser.getVersion(); | ||
var event = new Event('open'); | ||
event.initEvent('open', false, false); | ||
this.dispatchEvent(event); | ||
var self = this; | ||
this._stream.addListener('data', function(data) { | ||
@@ -68,2 +75,3 @@ var response = self._parser.parse(data); | ||
try { self._stream.write(response, 'binary') } catch (e) {} | ||
self._open(); | ||
}); | ||
@@ -75,2 +83,7 @@ ['close', 'end', 'error'].forEach(function(event) { | ||
WebSocket.prototype.ping = function(message, callback, context) { | ||
if (!this._parser.ping) return false; | ||
return this._parser.ping(message, callback, context); | ||
}; | ||
for (var key in API) WebSocket.prototype[key] = API[key]; | ||
@@ -77,0 +90,0 @@ |
@@ -10,2 +10,17 @@ var EventTarget = require('./api/event_target'), | ||
_open: function() { | ||
if (this._parser && !this._parser.isOpen()) return; | ||
this.readyState = API.OPEN; | ||
var buffer = this._sendBuffer || [], | ||
message; | ||
while (message = buffer.shift()) | ||
this.send.apply(this, message); | ||
var event = new Event('open'); | ||
event.initEvent('open', false, false); | ||
this.dispatchEvent(event); | ||
}, | ||
receive: function(data) { | ||
@@ -20,3 +35,14 @@ if (this.readyState !== API.OPEN) return false; | ||
send: function(data, type, errorType) { | ||
if (this.readyState === API.CLOSED) return false; | ||
if (this.readyState === API.CONNECTING) { | ||
if (this._sendBuffer) { | ||
this._sendBuffer.push(arguments); | ||
return true; | ||
} else { | ||
throw new Error('Cannot call send(), socket is not open yet'); | ||
} | ||
} | ||
if (this.readyState === API.CLOSED) | ||
return false; | ||
var frame = this._parser.frame(data, type, errorType); | ||
@@ -39,2 +65,3 @@ try { | ||
this.readyState = API.CLOSED; | ||
if (this._pingLoop) clearInterval(this._pingLoop); | ||
this._stream.end(); | ||
@@ -41,0 +68,0 @@ var event = new Event('close', {code: code || 1000, reason: reason || ''}); |
@@ -21,3 +21,3 @@ var net = require('net'), | ||
connection = secure | ||
? tls.connect(this._uri.port || 443, this._uri.hostname, onConnect) | ||
? tls.connect(this._uri.port || 443, this._uri.hostname, {}, onConnect) | ||
: net.createConnection(this._uri.port || 80, this._uri.hostname); | ||
@@ -36,6 +36,5 @@ | ||
}); | ||
connection.addListener('close', function() { | ||
self.close(1006, '', false); | ||
['close', 'end', 'error'].forEach(function(event) { | ||
connection.addListener(event, function() { self.close(1006, '', false) }); | ||
}); | ||
connection.addListener('error', function() {}); | ||
}; | ||
@@ -42,0 +41,0 @@ |
@@ -20,2 +20,6 @@ var Draft75Parser = function(webSocket) { | ||
isOpen: function() { | ||
return true; | ||
}, | ||
parse: function(buffer) { | ||
@@ -22,0 +26,0 @@ var data, message, value; |
@@ -50,5 +50,8 @@ var crypto = require('crypto'), | ||
Draft76Parser.prototype.isOpen = function() { | ||
return !!this._handshakeComplete; | ||
}; | ||
Draft76Parser.prototype.handshakeSignature = function(head) { | ||
if (head.length === 0) return null; | ||
this._handshakeComplete = true; | ||
@@ -69,2 +72,3 @@ var request = this._socket.request, | ||
this._handshakeComplete = true; | ||
return new Buffer(MD5.digest('binary'), 'binary'); | ||
@@ -71,0 +75,0 @@ }; |
@@ -13,2 +13,4 @@ var crypto = require('crypto'), | ||
this._pingCallbacks = {}; | ||
if (typeof this._protocols === 'string') | ||
@@ -101,2 +103,6 @@ this._protocols = this._protocols.split(/\s*,\s*/); | ||
isOpen: function() { | ||
return true; | ||
}, | ||
createHandshake: function(uri) { | ||
@@ -244,2 +250,8 @@ return new Handshake(uri, this._protocols); | ||
ping: function(message, callback, context) { | ||
message = message || ''; | ||
if (callback) this._pingCallbacks[message] = [callback, context]; | ||
return this._socket.send(message, 'ping'); | ||
}, | ||
close: function(code, reason, callback, context) { | ||
@@ -310,2 +322,10 @@ if (this._closed) return; | ||
} | ||
else if (opcode === this.OPCODES.pong) { | ||
var callbacks = this._pingCallbacks, | ||
message = this._encode(payload), | ||
callback = callbacks[message]; | ||
delete callbacks[message]; | ||
if (callback) callback[0].call(callback[1]); | ||
} | ||
}, | ||
@@ -312,0 +332,0 @@ |
@@ -7,3 +7,3 @@ { "name" : "faye-websocket" | ||
, "version" : "0.3.1" | ||
, "version" : "0.4.0" | ||
, "engines" : {"node": ">=0.4.0"} | ||
@@ -13,2 +13,4 @@ , "main" : "./lib/faye/websocket" | ||
, "scripts" : {"test": "node spec/runner.js"} | ||
, "bugs" : "http://github.com/faye/faye-websocket-node/issues" | ||
@@ -15,0 +17,0 @@ |
Sorry, the diff of this file is not supported yet
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
70844
1393
1
249
27