Skip to content

Instantly share code, notes, and snippets.

@JamsMendez
Created September 25, 2014 14:31
Show Gist options
  • Save JamsMendez/77b9c19800eaee41cd72 to your computer and use it in GitHub Desktop.
Save JamsMendez/77b9c19800eaee41cd72 to your computer and use it in GitHub Desktop.
Factory para implementar Socket.IO y Angular.js
app.factory('SocketIO', 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);
}
});
})
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment