// Reset the disconnect_requested flag\r
that.disconnect_requested = false;\r
\r
+ // Each minute we need to trigger a heartbeat. Server expects 2min, but to be safe we do it every 1min\r
+ var heartbeat = function() {\r
+ if (!that.rpc) return;\r
+\r
+ that.rpc('kiwi.heartbeat');\r
+ that._heartbeat_tmr = setTimeout(heartbeat, 60000);\r
+ };\r
+\r
+ heartbeat();\r
+\r
console.log("_kiwi.gateway.socket.on('open')");\r
});\r
\r
// Handles the kiwi.* RPC functions
this.attachKiwiCommands();
+ websocket.on('message', function() {
+ // A message from the client is a sure sign the client is still alive, so consider it a heartbeat
+ that.heartbeat();
+ });
+
websocket.on('close', function () {
websocketDisconnect.apply(that, arguments);
});
Client.prototype.dispose = function () {
Stats.incr('client.disposed');
- this.disposed = true;
+ if (this._heartbeat_tmr) {
+ clearTimeout(this._heartbeat_tmr);
+ }
+
this.rpc.dispose();
+ this.websocket.removeAllListeners();
+
+ this.disposed = true;
this.emit('dispose');
+
this.removeAllListeners();
};
+Client.prototype.heartbeat = function() {
+ if (this._heartbeat_tmr) {
+ clearTimeout(this._heartbeat_tmr);
+ }
+
+ // After 2 minutes of this heartbeat not being called again, assume the client has disconnected
+ console.log('resetting heartbeat');
+ this._heartbeat_tmr = setTimeout(_.bind(this._heartbeat_timeout, this), 120000);
+};
+
+
+Client.prototype._heartbeat_timeout = function() {
+ console.log('heartbeat stopped');
+ Stats.incr('client.timeout');
+ this.dispose();
+};
+
+
+
Client.prototype.attachKiwiCommands = function() {
var that = this;
build_version: args.build_version.toString() || undefined
};
});
+
+
+ // Just to let us know the client is still there
+ this.rpc.on('kiwi.heartbeat', function(callback, args) {
+ that.heartbeat();
+ });
};