javascript - Socket.io does not connect when using $location.path() in angular -


i want use socket.io @ 1 section of webapp. use socket.io's set('authorization', fn) handshaking current context(express session , page-id). works great when refresh current page, not when routing handled $location.path(url).

socket.io code(view controller):

.controller('viewctrl', function ($scope, $routeparams, $http, $location, socket) {   socket.connect('', { query: 'id=' + $routeparams.id });   socket.emit('msg', { data: 'key'}); //debug   socket.on('connect', function(){     console.log('socket connected');   });   socket.on('disconnect', function(){     console.log('socket disconnected');   });   ... 

routing code(main controller, frontpage):

if (data.owner == "yes") $location.path('/view/' + $scope.id()); 

routing of course secured server-side also.

socket.io service:

.factory('socket', function ($rootscope) {   var disconnecting = false;   var socket = {};   return {     connect: function(url, query){       disconnecting = false;       socket = io.connect(url, query);     },     on: function(eventname, callback){       socket.on(eventname, function(){         var args = arguments;         if(!disconnecting){           $rootscope.$apply(function(){             callback.apply(socket, args);           });         }         else {           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);           }         });       })     },     disconnect: function(){       disconnecting = true;       socket.disconnect();     },     socket: socket   }; }); 

i've tried using socket.io directly in controller(no service), same outcome.

the console not give errors either.

one solution using socket.io whole app, communicating page-id, etc emits. seem waste when need socket.io @ "view-page".

edit: goes wrong(should not have omitted this):

$scope.$on('$destroy', function (event) {   // disconnect socket when leaving page   socket.disconnect(); }); 

socket.io not work in manner connect() -> disconnect() -> connect().

when creating 1 page app, io in namespace , socket not removed io.sockets. therefor new socket not created @ second connect().

look @ line 93 in socket.io client library:

if (options['force new connection'] || !io.sockets[uuri]) {   socket = new io.socket(options); } 

solution, add 'force new connection':

var socket = io.connect('', { 'force new connection': true, query: 'id=' + $routeparams.id }); 

thanks shaunhusain pointing me chrome debugging!

reference: https://github.com/learnboost/socket.io-client/issues/251


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -