B
B
Bogdan Bembenok2016-04-10 04:21:18
JavaScript
Bogdan Bembenok, 2016-04-10 04:21:18

Why does socket.on increase by 1 every time the controller is reloaded?

The controller itself (socket.removeAllListeners didn't help, I tried calling it without eventName):

.controller('chatNotificationCtrl', ['$scope', 'socket', 'toastr', '$http', function($scope, socket, toastr, $http) {
  $scope.chatNotifications = {};

  if ($scope.logged) {
    $scope.chatNotifications = $scope.logged.chatNotify;
  } else {
    return;
  }
  console.log('STARTED');
  socket.on('notify:chat', function(data) {
    console.log('NOTIFY CHAT');
    $scope.chatNotifications.unshift(data);
    toastr.success('Example', 'New Message');
  });
  $scope.$on('$destroy', function() {
    console.log('!!!!!!!!!!!!')
    socket.removeAllListeners('notify:chat');
  });
}])

And Factory:
.factory('socket', ['$rootScope', function($rootScope) {
    var socket = io.connect();
    return {
      id: socket.id,
      on: function(eventName, callback) {
        socket.on(eventName, function() {
          console.log(socket);
          var args = arguments;
          $rootScope.$apply(function() {
            callback.apply(socket, args);
          });
        });
      },
      emit: function(eventName, data, callback) {
        socket.emit(eventName, data, function() {
          var args = arguments;
          $rootScope.$apply(function() {
            if (callback) {
              callback.apply(socket, args);
            }
          });
        })
      },
      removeAllListeners: function(eventName, callback) {
        socket.removeAllListeners(eventName, function() {
          var args = arguments;
          $rootScope.$apply(function() {
            callback.apply(socket, args);
          });
        });
      }
    };
  }])

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question