Giter VIP home page Giter VIP logo

Comments (6)

lrice avatar lrice commented on June 9, 2024 1

My factory looks something like this:

.factory('Socket', ['socketFactory', function(Socket){

    return function(){
      var socket = io.connect('<URL>', {forceNew: true});
      return Socket({ioSocket:socket});
    };

}]);

And when I disconnect, I have tried

.controller('somecontroller',['$scope','Socket',
  function ($scope,Socket){
    $scope.socket = Socket();
   ...

    $scope.on('$destroy', function(){
        $scope.socket.disconnect();
        and
        $scope.socket.disconnect(true);
    };
};

Is there a difference between ioSocket and the disconnect exposed by socketFactory? Have you tried the situation where there isn't a current connection to the server? That was the main issue for me, if I shut down the server while the connection was active, and THEN tried to disconnect, I could not prevent it from retrying infinitely (other than to put a cap on retry attempts, etc). I will try your method when I get a chance and see if that has an affect on my situation, but I was afraid that it was just a symptom of something like what @davisford was describing.

from angular-socket-io.

davisford avatar davisford commented on June 9, 2024

It is a little tricky with socket.io. Not sure what your needs are, but my app requires the socket to be torn down and thrown away on a logout, and a new one reconnected on a login. I've outlined how I do it in issue #86 along with a problem I have doing so with this library, and a workaround solution. Maybe it helps you?

from angular-socket-io.

lrice avatar lrice commented on June 9, 2024

Definitely, given that I'm not making use of the forwarding or scope broadcast features, I may just wrap the socket myself or fork this repo rather than modifying the source though. Thanks for the info though, its definitely good to know why this was occurring.

from angular-socket-io.

redders6600 avatar redders6600 commented on June 9, 2024

Can you clarify how you are calling disconnect?

Exposing the socket's disconnect method via my service seems to work fine for me:

.factory('Socket', (socketFactory)->

  ioSocket = io.connect('/', {})

  socketFactory = socketFactory({ioSocket: ioSocket})
  socketFactory.disconnect = ()->
    ioSocket.disconnect()

  return socketFactory
)

In some controller:

.controller('SomeController', (Socket)->
  $scope.disconnect =-> Socket.disconnect()
)

from angular-socket-io.

davisford avatar davisford commented on June 9, 2024

@LukeRiceApperson I think I see your problem. You don't need to execute a fn/ctor on a dependency that is injected (like an angular service or factory). Angular already instantiates those for you.

So, I think your controller ought to instead be something like this:

.controller('somecontroller', ['$scope', 'Socket', function ($scope, Socket) {
    // isn't necessary to hang Socket off $scope unless you need to use it in an angular view expression
    $scope.socket = Socket;

    $scope.on('$destroy', function () {
      Socket.disconnect();
    });
}

That should work. There's another tip for debugging socket.io, run this in the console and then restart your app:

window.localStorage.debug = '*'; // that's an asterisk *

This will dump all kinds of debug info to the console on what socket.io / engine.io is doing.

from angular-socket-io.

redders6600 avatar redders6600 commented on June 9, 2024

Is there a difference between ioSocket and the disconnect exposed by socketFactory?

Probably not - when I wrote that code this library wasn't exposing a disconnect method, so if it's implemented now it's likely the same.

I think @davisford has hit the nail on the head with regards to your problem.

from angular-socket-io.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.