Comparing version 0.3.0 to 0.4.0
@@ -38,2 +38,3 @@ 'use strict'; | ||
var CLOSE_BAD_KEY = 4403; | ||
var CLOSE_TIMEOUT = 1011; | ||
@@ -81,2 +82,10 @@ | ||
/** | ||
* @property idle | ||
* @type Object | ||
*/ | ||
reader(this, 'idle', { | ||
delay: Stream.IDLE_TIMEOUT | ||
}); | ||
/** | ||
* @property subscriptions | ||
@@ -87,2 +96,10 @@ * @type Subscriptions | ||
this.open = this.open.bind(this); | ||
this.opened = this.opened.bind(this); | ||
this.closed = this.closed.bind(this); | ||
this.ping = this.ping.bind(this); | ||
this.pong = this.pong.bind(this); | ||
this.receive = this.receive.bind(this); | ||
this.error = this.error.bind(this); | ||
this.open(); | ||
@@ -94,2 +111,4 @@ } | ||
Stream.DEFAULT_RETRY = 10000; | ||
Stream.KEEP_ALIVE = 30000; | ||
Stream.IDLE_TIMEOUT = 60000; | ||
@@ -145,6 +164,7 @@ Stream.defaults = { | ||
this.socket | ||
.on('open', this.opened.bind(this)) | ||
.on('close', this.closed.bind(this)) | ||
.on('message', this.receive.bind(this)) | ||
.on('error', this.error.bind(this)); | ||
.on('open', this.opened) | ||
.on('close', this.closed) | ||
.on('message', this.receive) | ||
.on('pong', this.pong) | ||
.on('error', this.error); | ||
@@ -165,2 +185,15 @@ return this; | ||
if (Stream.KEEP_ALIVE) { | ||
this.socket._socket.setKeepAlive(true, Stream.KEEP_ALIVE); | ||
} | ||
if (this.idle.timeout) { | ||
clearTimeout(this.idle.timeout); | ||
delete this.idle.timeout; | ||
} | ||
if (this.idle.delay) { | ||
this.idle.interval = setInterval(this.ping, this.idle.delay); | ||
} | ||
if (this.retry.timeout) { | ||
@@ -208,5 +241,15 @@ clearTimeout(this.retry.timeout); | ||
this.retry.timeout = setTimeout(this.open.bind(this), this.retry.delay); | ||
this.retry.timeout = setTimeout(this.open, this.retry.delay); | ||
} | ||
if (this.idle.timeout) { | ||
clearTimeout(this.idle.timeout); | ||
delete this.idle.timeout; | ||
} | ||
if (this.idle.interval) { | ||
clearInterval(this.idle.interval); | ||
delete this.idle.interval; | ||
} | ||
this.emit('close', code, message); | ||
@@ -314,2 +357,28 @@ | ||
Stream.prototype.ping = function () { | ||
assert(this.socket); | ||
if (this.idle.timeout) { | ||
clearTimeout(this.idle.timeout); | ||
delete this.idle.timeout; | ||
} | ||
this.socket.ping(); | ||
this.idle.timeout = setTimeout(function () { | ||
debug('pong timed out, trying to close socket...'); | ||
this.close(CLOSE_TIMEOUT); | ||
}.bind(this), this.idle.delay / 2); | ||
return this; | ||
}; | ||
Stream.prototype.pong = function () { | ||
if (this.idle.timeout) { | ||
clearTimeout(this.idle.timeout); | ||
delete this.idle.timeout; | ||
} | ||
}; | ||
// --- Public Methods --- | ||
@@ -323,7 +392,10 @@ | ||
*/ | ||
Stream.prototype.close = function () { | ||
Stream.prototype.close = function (code) { | ||
if (this.socket) { | ||
if (this.socket.readyState < WS.CLOSING) { | ||
debug('closing websocket...'); | ||
this.socket.close(CLOSE_NORMAL); | ||
debug('closing websocket...'); | ||
this.socket.close(code || CLOSE_NORMAL); | ||
if (this.idle.interval) { | ||
clearInterval(this.idle.interval); | ||
delete this.idle.interval; | ||
} | ||
@@ -330,0 +402,0 @@ } |
{ | ||
"name": "zotero", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Zotero API client", | ||
@@ -33,3 +33,3 @@ "main": "lib/zotero.js", | ||
"mocha": "^2.4.5", | ||
"nock": "^7.2.2", | ||
"nock": "^8.0.0", | ||
"should": "^8.2.2", | ||
@@ -36,0 +36,0 @@ "sinon": "^1.17.3", |
100848
2070