Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket.removeListener not working #152

Open
swapnil001 opened this issue Dec 16, 2016 · 4 comments
Open

socket.removeListener not working #152

swapnil001 opened this issue Dec 16, 2016 · 4 comments

Comments

@swapnil001
Copy link

swapnil001 commented Dec 16, 2016

Hi,

I am trying to remove one listener when my controller is changing via

$scope.$on('$destroy', function() {
            hpSocketFactory.removeListener(emiterListernerLeagueLobby, function(err) {
            });
            hpSDSocketFactory.removeListener(emiterListernerMatchID, function(err) {

            });
        });

But it does not seem to work. Listener is not removed. How to remove one particular listener when controller is changing?

@melliott03
Copy link

I'm experiencing the same issue. Did you find a solution?

@swapnil001
Copy link
Author

No. It did not work. I had to change my system to work with removeAllListener property and on controller exit remove all listeners.

@melliott03
Copy link

I came up with the following solution to make removeListener work if you're interested. If involves not angular-socket-io and writing a factory to export socket.io. I think it's easier and more straightforward and it works.

@melliott03
Copy link

  1. include the cdn in your html
<script src="https://cdn.socket.io/socket.io-1.4.3.js"></script>
  1. Create a factory
myApp.factory('Socket', ['$rootScope', function ($rootScope) {
  var socket = io.connect();
  return {
    on: function (eventName, callback) {

      function wrapper() {
        var args = arguments;
        $rootScope.$apply(function () {
          callback.apply(socket, args);
        });
      }

      socket.on(eventName, wrapper);

      return function () {
        socket.removeListener(eventName, wrapper);
      };
    },

    emit: function (eventName, data, callback) {

      socket.emit(eventName, data, function () {
        var args = arguments;
        $rootScope.$apply(function () {
          if(callback) {
            callback.apply(socket, args);
          }
        });
      });
    },

    off: function (eventName) {

      function wrapper() {
        var args = arguments;
        $rootScope.$apply(function () {
          callback.apply(socket, args);
        });
      }

      socket.off(eventName);

      return function () {
        socket.removeListener(eventName, wrapper);
      };
    },

    removeAllListeners: function (eventName) {
      socket.removeAllListeners(eventName);

      return function () {
        socket.removeAllListeners(eventName);
      };
    },

    addListener: function (eventName, callback) {
      function wrapper() {
        var args = arguments;
        $rootScope.$apply(function () {
          callback.apply(socket, args);
        });
      }

      socket.on(eventName, wrapper);

      return function () {
        socket.on(eventName, wrapper);
      };
    },

  };
}]);
  1. Inject the factory into your controller
  2. instead of socket.removeListener, use socket.off(eventName)
  3. uninstall angular-socket-io

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants