From f992f50d491f44e90f10d5f9d6999671c8c79185 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Tue, 6 Jan 2015 16:48:48 +0100 Subject: [PATCH] Moved heartbeat time to controller. --- .../js/controllers/mediastreamcontroller.js | 5 ++ static/js/mediastream/api.js | 59 ++++++++++--------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/static/js/controllers/mediastreamcontroller.js b/static/js/controllers/mediastreamcontroller.js index dc56b43b..afc9da49 100644 --- a/static/js/controllers/mediastreamcontroller.js +++ b/static/js/controllers/mediastreamcontroller.js @@ -580,6 +580,11 @@ define(['jquery', 'underscore', 'angular', 'bigscreen', 'moment', 'sjcl', 'moder } }); + // Start heartbeat timer. + $window.setInterval(function() { + mediaStream.api.heartbeat(5000, 11500) + }, 1000); + $scope.$on("active", function(event, currentcall) { console.info("Video state active (assuming connected)", currentcall.id); diff --git a/static/js/mediastream/api.js b/static/js/mediastream/api.js index dd210439..8214d31e 100644 --- a/static/js/mediastream/api.js +++ b/static/js/mediastream/api.js @@ -22,9 +22,6 @@ "use strict"; define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { - var alive_check_timeout = 5000; - var alive_check_timeout_2 = 10000; - var Api = function(version, connector) { this.version = version; this.id = null; @@ -48,35 +45,39 @@ define(['jquery', 'underscore', 'ua-parser'], function($, _, uaparser) { this.received(data); }, this)); - // Trigger alive heartbeat when nothing is received for a while. - this.heartbeat = window.setInterval(_.bind(function() { - var last_receive = this.last_receive; - if (this.connector.connected) { - if (last_receive !== null) { - //console.log("api heartbeat", this.last_receive); - var now = new Date().getTime(); - if (this.last_receive_overdue) { - if (now > last_receive + alive_check_timeout_2) { - console.log("Reconnecting because alive timeout was reached."); - this.last_receive_overdue = false; - this.last_receive = null; - this.connector.disconnect(true); - } - } else { - if (now > last_receive + alive_check_timeout) { - //console.log("overdue 1"); - this.last_receive_overdue = true; - this.sendAlive(now); - } + // Heartbeat support. + this.last_receive = null; + this.last_receive_overdue = false; + + }; + + Api.prototype.heartbeat = function(timeout, timeout2) { + + // Heartbeat emitter. + var last_receive = this.last_receive; + if (this.connector.connected) { + if (last_receive !== null) { + //console.log("api heartbeat", this.last_receive); + var now = new Date().getTime(); + if (this.last_receive_overdue) { + if (now > last_receive + timeout2) { + console.log("Reconnecting because alive timeout was reached."); + this.last_receive_overdue = false; + this.last_receive = null; + this.connector.disconnect(true); + } + } else { + if (now > last_receive + timeout) { + //console.log("overdue 1"); + this.last_receive_overdue = true; + this.sendAlive(now); } } - } else { - this.last_receive = null; - this.last_receive_overdue = false; } - }, this), 1000); - this.last_receive = null; - this.last_receive_overdue = false; + } else { + this.last_receive = null; + this.last_receive_overdue = false; + } };