diff --git a/static/js/app.js b/static/js/app.js index d50bee2d..62e7b9d2 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -86,10 +86,15 @@ define([ $locationProvider.html5Mode(true); }]); - app.run(["$rootScope", "mediaStream", "translation", function($rootScope, mediaStream, translation) { + app.run(["$rootScope", "$timeout", "mediaStream", "translation", "continueConnector", function($rootScope, $timeout, mediaStream, translation, continueConnector) { translation.inject($rootScope); console.log("Initializing ..."); + var initialize = continueConnector.defer(); mediaStream.initialize($rootScope, translation); + $timeout(function() { + console.log("Initializing complete.") + initialize.resolve(); + }, 0); }]); app.constant("availableLanguages", languages); diff --git a/static/js/controllers/mediastreamcontroller.js b/static/js/controllers/mediastreamcontroller.js index fb25ec23..001ba75c 100644 --- a/static/js/controllers/mediastreamcontroller.js +++ b/static/js/controllers/mediastreamcontroller.js @@ -535,7 +535,8 @@ define(['underscore', 'bigscreen', 'moment', 'sjcl', 'modernizr', 'webrtc.adapte _.delay(function() { if (autoreconnect && !reconnecting) { reconnecting = true; - mediaStream.connector.reconnect() + console.log("Requesting to reconnect ..."); + mediaStream.reconnect(); } }, 500); $scope.setStatus("reconnecting"); diff --git a/static/js/directives/usability.js b/static/js/directives/usability.js index 217d1ffd..8ea6d259 100644 --- a/static/js/directives/usability.js +++ b/static/js/directives/usability.js @@ -24,7 +24,7 @@ define(['jquery', 'underscore', 'text!partials/usability.html'], function($, _, return ["mediaStream", function(mediaStream) { - var controller = ['$scope', "mediaStream", "safeApply", "$timeout", "localStorage", function($scope, mediaStream, safeApply, $timeout, localStorage) { + var controller = ['$scope', "mediaStream", "safeApply", "$timeout", "localStorage", "continueConnector", function($scope, mediaStream, safeApply, $timeout, localStorage, continueConnector) { var pending = true; var complete = false; @@ -37,12 +37,15 @@ define(['jquery', 'underscore', 'text!partials/usability.html'], function($, _, }; ctrl.setInfo("waiting"); + var continueDeferred = continueConnector.defer(); + $scope.continueConnect = function(status) { safeApply($scope, function() { pending = false; if (status) { localStorage.setItem("mediastream-mediacheck", MEDIA_CHECK) - $scope.connect() + console.log("Continue with connect after media check ..."); + continueDeferred.resolve(); if (mediaStream.config.DefaultRoomEnabled !== true) { ctrl.setInfo("initializing"); initializer = $timeout(function() { @@ -81,7 +84,7 @@ define(['jquery', 'underscore', 'text!partials/usability.html'], function($, _, // Toplevel watcher for connect function to become available. $scope.$watch("connect", function() { if ($scope.connect) { - console.log("Connecting ..."); + console.log("Checking for media access ..."); ctrl.setInfo("checking"); $timeout(function() { if (pending) { diff --git a/static/js/services/continueconnector.js b/static/js/services/continueconnector.js new file mode 100644 index 00000000..b6c65a9b --- /dev/null +++ b/static/js/services/continueconnector.js @@ -0,0 +1,120 @@ +/* + * Spreed WebRTC. + * Copyright (C) 2013-2014 struktur AG + * + * This file is part of Spreed WebRTC. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +define(['underscore'], function(_) { + + // Helper class to kill of old defers. + var Caller = function(ref) { + this.ref = ref; + }; + Caller.prototype.disable = function() { + this.ref = null; + } + Caller.prototype.resolve = function(value) { + if (this.ref) { + _.bind(this.ref.resolve, this.ref)(value); + } + }; + Caller.prototype.reject = function(reason) { + if (this.ref) { + _.bind(this.ref.reject, this.ref)(reason); + } + }; + Caller.prototype.notify = function(value) { + if (this.ref) { + _.bind(this.ref.notify, this.ref)(value); + } + }; + + // continueConnector + return ["$q", function($q) { + + var ContinueConnector = function() { + this.deferred = $q.defer(); + this.promises = []; + this.thens = []; + this.current = null; + }; + + ContinueConnector.prototype.add = function(promise) { + + this.promises.push(promise); + this.refresh(); + + }; + + ContinueConnector.prototype.remove = function(promise) { + + this.promises = _.without(this.promises, promise); + this.refresh(); + + }; + + ContinueConnector.prototype.refresh = function() { + + // Disable old ones. + if (this.current) { + this.current.disable(); + } + // Prepare caller. + var current = this.current = new Caller(this); + // Create new promise for all registered promises. + var all = $q.all(this.promises); + all.then(_.bind(current.resolve, current), _.bind(current.reject, current), _.bind(current.notify, current)); + }; + + ContinueConnector.prototype.defer = function() { + var deferred = $q.defer(); + this.add(deferred.promise); + return deferred; + }; + + ContinueConnector.prototype.resolve = function(value) { + //console.log("Continue connector resolved", arguments); + this.deferred.resolve(value); + }; + + ContinueConnector.prototype.reject = function(reason) { + this.deferred.reject(reason); + }; + + ContinueConnector.prototype.notify = function(value) { + this.deferred.notify(value); + }; + + ContinueConnector.prototype.then = function(successCallback, errorCallback, notifyCallback) { + this.thens.push(arguments); + return this.deferred.promise.then(successCallback, errorCallback, notifyCallback); + }; + + ContinueConnector.prototype.reset = function() { + this.deferred = $q.defer(); + var p = this.deferred.promise; + _.each(this.thens, function(args) { + p.then.apply(p, args); + }); + this.refresh(); + }; + + return new ContinueConnector(); + + }]; + +}); diff --git a/static/js/services/mediastream.js b/static/js/services/mediastream.js index af1b11f4..5fc9a667 100644 --- a/static/js/services/mediastream.js +++ b/static/js/services/mediastream.js @@ -30,7 +30,7 @@ define([ ], function($, _, uaparser, Modernizr, Connector, Api, WebRTC, tokens) { - return ["globalContext", "$route", "$location", "$window", "visibility", "alertify", "$http", "safeApply", "$timeout", "$sce", "localStorage", function(context, $route, $location, $window, visibility, alertify, $http, safeApply, $timeout, $sce, localStorage) { + return ["globalContext", "$route", "$location", "$window", "visibility", "alertify", "$http", "safeApply", "$timeout", "$sce", "localStorage", "continueConnector", function(context, $route, $location, $window, visibility, alertify, $http, safeApply, $timeout, $sce, localStorage, continueConnector) { var url = (context.Ssl ? "wss" : "ws") + "://" + context.Host + (context.Cfg.B || "/") + "ws"; var version = context.Cfg.Version || "unknown"; @@ -194,6 +194,18 @@ define([ localStorage.removeItem("mediastream-login-" + context.Cfg.UsersMode); } }, + connect: function() { + continueConnector.then(function() { + console.log("Connecting ..."); + connector.connect(url); + }); + }, + reconnect: function() { + continueConnector.then(function() { + console.log("Reconnecting ..."); + connector.reconnect(); + }); + }, initialize: function($rootScope, translation) { var cont = false; @@ -203,6 +215,7 @@ define([ $rootScope.roomid = null; $rootScope.roomlink = null; $rootScope.roomstatus = false; + $rootScope.connect = false; var connect = function() { // We need websocket support to connect. @@ -212,10 +225,10 @@ define([ } if (ready && cont) { // Inject connector function into scope, so that controllers can pick it up. + console.log("Ready to connect ..."); + mediaStream.connect(); safeApply($rootScope, function(scope) { - scope.connect = function() { - connector.connect(url); - }; + scope.connect = true; }); } }; diff --git a/static/js/services/services.js b/static/js/services/services.js index fd18dd33..f29f9e97 100644 --- a/static/js/services/services.js +++ b/static/js/services/services.js @@ -52,7 +52,8 @@ define([ 'services/animationframe', 'services/dialogs', 'services/geolocation', - 'services/screensharing'], function(_, + 'services/screensharing', + 'services/continueconnector'], function(_, desktopNotify, playSound, safeApply, @@ -84,7 +85,8 @@ localStorage, animationFrame, dialogs, geolocation, -screensharing) { +screensharing, +continueConnector) { var services = { desktopNotify: desktopNotify, @@ -118,7 +120,8 @@ screensharing) { animationFrame: animationFrame, dialogs: dialogs, geolocation: geolocation, - screensharing: screensharing + screensharing: screensharing, + continueConnector: continueConnector }; var initialize = function(angModule) {