Skip to content

Instantly share code, notes, and snippets.

@Micka33
Created April 10, 2014 09:43
Show Gist options
  • Save Micka33/10362393 to your computer and use it in GitHub Desktop.
Save Micka33/10362393 to your computer and use it in GitHub Desktop.
Angularjs wrapper for socket.io
admin.factory('$socket', ['$rootScope', function ($rootScope) {
if (typeof io != 'undefined')
var socket = io.connect();
return {
connect: function (url) {
if (typeof io != 'undefined')
socket = io.connect(url);
},
on: function (eventName, callback) {
if (typeof socket != 'undefined')
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
emit: function (eventName, data, callback) {
if (typeof socket != 'undefined')
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