lundi 16 mars 2015

Angularjs socket.on event not working when socket changed in factory


Vote count:

0




I am having a factory for socket.io, If the user not loggedin then I am having



var socket = io.connect(SOCKET_URL);


When the user logs into the site, then I am changing the socket value.



socket = io.connect(SOCKET_URL, {forceNew: true});


In my controller I have



socket.on('notification', function(data){console.log(data);})


This socket.on('notification') is not working after changing the socket variable in the factory.


My factory



app.factory('socket', function($rootScope){
var socket = io.connect(SOCKET_URL);
return {
reconnect:function(){
console.log("reconnect");
socket = io.connect(SOCKET_URL, {forceNew: true});
},
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
}
});


My controllers



app.controller('myCtrl1', function($scope, socket){
$scope.login = function(){
// .............
socket.reconnect();
}
});


app.controller('myCtrl2', function($scope, socket){
socket.on('notification', function(data){ // this is not working
console.log(data); //after calling socket.reconnect();
})
});


asked 55 secs ago

Vishnu

150






Angularjs socket.on event not working when socket changed in factory

Aucun commentaire:

Enregistrer un commentaire