javascript - Socket.io which is in my Angularjs controller, is duplicated every time I access to the controller -


i have app node.js, angularjs , socket.io, have controller maps.html receives events other clients socket.io that:

app.controller('mapctrl', function ($scope, $http, socket) {  vm = this;  //....      socket.on('finalizarruta', function(data) {          //other function controller         vm.funciones.almacenarhistorialfinalruta();      });  });  

all work but, when im going other page, , later return maps.html, reactive controller , socket.io functions activated 2 or more times (depends number of returns controller).

i have see answer said have put socket.io functions out controller, in service but, functions executes other functions same controller.

is there way prevent socket.io functions running more once, without having remove them controller?

more code:

app.factory('socket', ['$rootscope', function($rootscope) {  var socket = io.connect();   return {       on: function (eventname, callback) {           socket.on(eventname, function () {               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);                   }               });           });       },       getsocket: function() {         return socket;       }    }; }]); 


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -