diff --git a/angular_app/js/controllers.js b/angular_app/js/controllers.js index 3c57136..01193e4 100644 --- a/angular_app/js/controllers.js +++ b/angular_app/js/controllers.js @@ -74,6 +74,7 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', 'Event', 'Person', // Handle WebSocket connection used to update the list of persons. $scope.EventUpdates = EventUpdates; + $scope.EventUpdates.open(); $scope.$watchCollection(function() { return $scope.EventUpdates.data; }, function(prev, old) { @@ -197,6 +198,11 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', 'Event', 'Person', $scope.showMessage = function(cfg) { $scope.message.show(cfg); }; + + $scope.$on('$destroy', function() { + $log.debug('destroying controller'); + $scope.EventUpdates && $scope.EventUpdates.close(); + }); }] ); diff --git a/angular_app/js/services.js b/angular_app/js/services.js index 23f46d9..d641ea8 100644 --- a/angular_app/js/services.js +++ b/angular_app/js/services.js @@ -137,20 +137,29 @@ eventManServices.factory('Setting', ['$resource', /* WebSocket collection used to update the list of persons of an Event. */ eventManApp.factory('EventUpdates', ['$websocket', '$location', '$log', function($websocket, $location, $log) { - var proto = $location.protocol() == 'https' ? 'wss' : 'ws'; - var dataStream = $websocket(proto + '://' + $location.host() + ':' + $location.port() + - '/ws/' + $location.path() + '/updates'); + var dataStream = null; var data = {}; - dataStream.onMessage(function(message) { - $log.debug('EventUpdates message received'); - data.persons = angular.fromJson(message.data); - }); - var methods = { data: data, + close: function() { + $log.debug('close WebSocket connection'); + dataStream.close(); + }, + open: function() { + $log.debug('open WebSocket connection'); + dataStream && dataStream.close(); + var proto = $location.protocol() == 'https' ? 'wss' : 'ws'; + dataStream = $websocket(proto + '://' + $location.host() + ':' + $location.port() + + '/ws/' + $location.path() + '/updates'); + dataStream.onMessage(function(message) { + $log.debug('EventUpdates message received'); + data.persons = angular.fromJson(message.data); + }); + + } }; return methods;