Let keepalive listen to socket events

Wait until the socket connects before starting the keepalive timer.
Automatically stop the keepalive when the socket is closed.

// FREEBIE
This commit is contained in:
lilia 2015-10-05 15:05:24 -07:00
parent 1345899253
commit 0ff38e41c2
2 changed files with 8 additions and 16 deletions

View file

@ -38456,22 +38456,19 @@ axolotlInternal.RecipientRecord = function() {
reader.readAsArrayBuffer(blob); reader.readAsArrayBuffer(blob);
}; };
var keepalive;
if (opts.keepalive) { if (opts.keepalive) {
keepalive = new KeepAlive(this, { var keepalive = new KeepAlive(this, {
path : opts.keepalive.path, path : opts.keepalive.path,
disconnect : opts.keepalive.disconnect disconnect : opts.keepalive.disconnect
}); });
this.resetKeepAliveTimer = keepalive.reset.bind(keepalive); this.resetKeepAliveTimer = keepalive.reset.bind(keepalive);
socket.addEventListener('connect', this.resetKeepAliveTimer);
socket.addEventListener('close', keepalive.stop.bind(keepalive));
} }
this.close = function(code, reason) { this.close = function(code, reason) {
if (!code) { code = 3000; } if (!code) { code = 3000; }
socket.close(code, reason); socket.close(code, reason);
if (keepalive) {
keepalive.close();
}
}; };
}; };
@ -38488,7 +38485,6 @@ axolotlInternal.RecipientRecord = function() {
this.disconnect = true; this.disconnect = true;
} }
this.wsr = websocketResource; this.wsr = websocketResource;
this.reset();
} else { } else {
throw new TypeError('KeepAlive expected a WebSocketResource'); throw new TypeError('KeepAlive expected a WebSocketResource');
} }
@ -38496,7 +38492,7 @@ axolotlInternal.RecipientRecord = function() {
KeepAlive.prototype = { KeepAlive.prototype = {
constructor: KeepAlive, constructor: KeepAlive,
close: function() { stop: function() {
clearTimeout(this.keepAliveTimer); clearTimeout(this.keepAliveTimer);
clearTimeout(this.disconnectTimer); clearTimeout(this.disconnectTimer);
}, },

View file

@ -129,22 +129,19 @@
reader.readAsArrayBuffer(blob); reader.readAsArrayBuffer(blob);
}; };
var keepalive;
if (opts.keepalive) { if (opts.keepalive) {
keepalive = new KeepAlive(this, { var keepalive = new KeepAlive(this, {
path : opts.keepalive.path, path : opts.keepalive.path,
disconnect : opts.keepalive.disconnect disconnect : opts.keepalive.disconnect
}); });
this.resetKeepAliveTimer = keepalive.reset.bind(keepalive); this.resetKeepAliveTimer = keepalive.reset.bind(keepalive);
socket.addEventListener('connect', this.resetKeepAliveTimer);
socket.addEventListener('close', keepalive.stop.bind(keepalive));
} }
this.close = function(code, reason) { this.close = function(code, reason) {
if (!code) { code = 3000; } if (!code) { code = 3000; }
socket.close(code, reason); socket.close(code, reason);
if (keepalive) {
keepalive.close();
}
}; };
}; };
@ -161,7 +158,6 @@
this.disconnect = true; this.disconnect = true;
} }
this.wsr = websocketResource; this.wsr = websocketResource;
this.reset();
} else { } else {
throw new TypeError('KeepAlive expected a WebSocketResource'); throw new TypeError('KeepAlive expected a WebSocketResource');
} }
@ -169,7 +165,7 @@
KeepAlive.prototype = { KeepAlive.prototype = {
constructor: KeepAlive, constructor: KeepAlive,
close: function() { stop: function() {
clearTimeout(this.keepAliveTimer); clearTimeout(this.keepAliveTimer);
clearTimeout(this.disconnectTimer); clearTimeout(this.disconnectTimer);
}, },