adding promise for successful socket connection

in order to prevent the a race condition: the socket was trying to emit
messages to the API to subscribe to rooms before the socket connection was
finshed connectig.
This commit is contained in:
Jared Tabor
2016-09-06 18:48:23 -07:00
parent 0937866e20
commit 9691e267df
2 changed files with 20 additions and 13 deletions

View File

@@ -711,7 +711,7 @@ var tower = angular.module('Tower', [
} }
$rootScope.removeOpenSocket = $rootScope.$on('OpenSocket', function() { $rootScope.removeOpenSocket = $rootScope.$on('OpenSocket', function() {
function openSocket() { function openSocket() {
$rootScope.socket = Socket({ scope: $rootScope}); // $rootScope.socket = Socket({ scope: $rootScope});
$rootScope.socket.init(); $rootScope.socket.init();
// $rootScope.socket.on("status_changed", function(data) { // $rootScope.socket.on("status_changed", function(data) {
// $log.debug('Job ' + data.unified_job_id + // $log.debug('Job ' + data.unified_job_id +
@@ -924,6 +924,10 @@ var tower = angular.module('Tower', [
// create a promise that will resolve when features are loaded // create a promise that will resolve when features are loaded
$rootScope.featuresConfigured = $q.defer(); $rootScope.featuresConfigured = $q.defer();
} }
if (!$rootScope.socketPromise) {
// create a promise that resolves when the socket connection is open
$rootScope.socketPromise = $q.defer();
}
$rootScope.licenseMissing = true; $rootScope.licenseMissing = true;
//the authorization controller redirects to the home page automatcially if there is no last path defined. in order to override //the authorization controller redirects to the home page automatcially if there is no last path defined. in order to override
// this, set the last path to /portal for instances where portal is visited for the first time. // this, set the last path to /portal for instances where portal is visited for the first time.

View File

@@ -3,10 +3,10 @@
* *
* All Rights Reserved * All Rights Reserved
*************************************************/ *************************************************/
import ReconnectingWebSocket from 'reconnectingwebsocket' import ReconnectingWebSocket from 'reconnectingwebsocket';
export default export default
['$rootScope', '$location', '$log', 'Authorization','$state', ['$rootScope', '$location', '$log', 'Authorization','$state', '$q',
function ($rootScope, $location, $log, Authorization, $state) { function ($rootScope, $location, $log, Authorization, $state, $q) {
return { return {
init: function() { init: function() {
var self = this, var self = this,
@@ -21,9 +21,10 @@ export default
timeoutInterval: 3000, timeoutInterval: 3000,
maxReconnectAttempts: 10 maxReconnectAttempts: 10
}); });
// self.socket.onopen = function () { self.socket.onopen = function () {
// console.log('websocket connected'); //log errors console.log('websocket connected'); //log errors
// }; $rootScope.socketPromise.resolve();
};
// //
// self.socket.onerror = function (error) { // self.socket.onerror = function (error) {
// console.log('Error Logged: ' + error); //log errors // console.log('Error Logged: ' + error); //log errors
@@ -154,12 +155,14 @@ export default
emit: function (eventName, data, callback) { emit: function (eventName, data, callback) {
var self = this; var self = this;
// console.log(eventName) // console.log(eventName)
self.socket.send(eventName, data, function () { $rootScope.socketPromise.promise.then(function(){
var args = arguments; self.socket.send(eventName, data, function () {
self.scope.$apply(function () { var args = arguments;
if (callback) { self.scope.$apply(function () {
callback.apply(self.socket, args); if (callback) {
} callback.apply(self.socket, args);
}
});
}); });
}); });
}, },