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

View file

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