I work with a socket.io node server and with a socket.io angular client (using angular-socket.io ) and it works well, except for one specific condition: I cannot figure out how to completely destroy a socket when I am done with it. when the socket could not connect.
The workflow is as follows: on this page there is a button for opening the modal file, opening the modal file inserts the factory socket into the modal controller and the connection is made to the socket.io server. All this works well when the socket can connect. When the modal function is closed, $ scope.destroy is called and the handler in the modal controller tries to clear the socket.
This modal can be closed and reopened several times or only once. My process of creating / destroying a socket several times may be part of my problem, it may not be the right paradigm, but when the modal is closed, there will be no way to interact with it, and after this page remains, the socket, of course, will not be needed to exist or to remain bound.
Creating a socket:
angular.module('app.common.newSocketFactory', ['btford.socket-io']) .factory('newSocketFactory', ['socketFactory', function(socketFactory){ return function(){ return socketFactory({ioSocket:io.connect('http://localhost:3000', {forceNew: true})}); }; }]);
Injection:
angular.module('...', [ 'app.common.newSocketFactory', .... ]) .controller('Controller', ['$scope', '$modalInstance', 'newSocketFactory', ... function ($scope, $modalInstance, newSocketFactory, ...){ ... $scope.socket = newSocketFactory();
Here is my destruction handler:
$scope.$on('$destroy', function(){ $scope.socket.emit('unlisten'); $scope.socket.disconnect();
This works correctly when the socket was able to connect before the modal was closed, a disconnected event on the server indicates that the client is disconnected. In addition, I can close modal and THEN to turn off the server, and no attempt to reconnect will occur.
When the server closes when the modal file is opened, TransportErrors transactions and attempts to reconnect occur. If at this moment I close the modality, the errors continue, and if I restart the server, the repetition will be successful and the client will connect to the server. In the worst case scenario, I use this socket to support atomic access to a piece of equipment that is blocked by the first socket. If I do not restart the server, allow the socket to connect, and then close the modal (running $ destroy in my selection area and socket.disconnect ()), I have to refresh the whole page to destroy the socket.
This is potentially an angular-socket.io question or a regular socket.io socket question. I asked github in angular-socket.io without an answer, how to get rid of a client socket angular-socket.io without an answer correctly.
My question is: A) If I do not do the main faux-pas with socket.io, how can I safely get rid of this client socket, and B) If I do it wrong, then what better way is to go to the website management, which should not be saved from page to page.
Note. The angular-socket.io connector simply wraps the native socket with some materials to help deal with angular areas. When you call the disconnect () function, the result is what looks like a main socket. I tried to set the reconnection settings to false, etc., but none of them worked, and I don’t like that the socket still seems to exist even after the region was completely destroyed, even if I CAN crack it to stop try to connect.
thanks