elghazal-a
elghazal-a

Reputation: 590

How can I force a disconnection in Socket.io?

I'm trying to disconnect manually my clients from my chat. Let me show my basic code :

...

<h1>welcome to my chat</h1>

<script>
$(function(){

    var mySocket = io.connect('/game1/roomGame);

    $('#force').click(function(event){
    mySocketdisconnect();
    })


});
</script>

In my server:

    mySocket.on('disconnect', function(){
             console.log(" disconnection forced ");
    }); 

In my console, when leaving the route /game1/roomGame , certainly, I see the message disconnection forced, but I'm sure the disconnection isn't really done because I see also in my console somes messages like:

   debug - emitting heartbeat for client f8oj63XfQ7T8mbNFP8SR
   debug - websocket writing 2::
   debug - set heartbeat timeout for client f8oj63XfQ7T8mbNFP8SR
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client f8oj63XfQ7T8mbNFP8SR
   debug - set heartbeat interval for client f8oj63XfQ7T8mbNFP8SR

How can I close literraly this connection ?

Upvotes: 0

Views: 138

Answers (1)

Andrew Joslin
Andrew Joslin

Reputation: 43023

You want to call mySocketRoom.socket.disconnect(). Expose that as disconnect on your service:

return {
  ...
  disconnect: function() { mySocketRoom.socket.disconnect(); }
};

Also, if your gameRoom lasts only as long as the user is in a certain controller, $locationChangeStart and $locationChangeSuccess seem too general.

I would just disconnect whenever the controller $scope is destroyed.

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

Documentation: http://docs.angularjs.org/api/ng.$rootScope.Scope#$destroy

Upvotes: 1

Related Questions