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() {
function openSocket() {
$rootScope.socket = Socket({ scope: $rootScope});
// $rootScope.socket = Socket({ scope: $rootScope});
$rootScope.socket.init();
// $rootScope.socket.on("status_changed", function(data) {
// $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
$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;
//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.

View File

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