Answer the question
In order to leave comments, you need to log in
Why does socket.on('custom event') work multiple times?
Good day everyone. There is a small project on angular + socket.io + nodejs
The application has only two routes
Example:
$routeProvider.
when('/', {
templateUrl: '/tpl',
controller: 'MainCtrl'
}).
when('/other', {
templateUrl: 'other.tpl',
controller: 'OtherCtrl'
}).
.otherwise({
redirectTo: '/'
});
socket.on( 'new user' , function ( data ){
console.log(1);
$scope.UserList.push(JSON.parse(data));
});
$scope.UserList.push(JSON.parse(data));
MyApp.factory('socket', ['$rootScope', function ($rootScope) {
var safeApply = function(scope, fn) {
if (scope.$$phase) {
fn();
} else {
scope.$apply(fn);
}
};
var socket = io.connect(URL_TO_SOCKET);
return {
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
safeApply($rootScope, function () {
callback.apply(socket, args);
});
});
},
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
safeApply($rootScope, function () {
if (callback) {
callback.apply(socket, args);
}
});
})
},
disconnect: function () {
socket.disconnect();
},
socket: socket
};
}]);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question