Created
May 22, 2014 03:20
-
-
Save Ellisande/97436c4e8cea7d8431b2 to your computer and use it in GitHub Desktop.
Angular Service Wrapped for Socket.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.factory('socket', function ($rootScope) { | |
'use strict'; | |
var socket; | |
var connect = function(){ | |
if(!socket){ | |
socket = io.connect('http://localhost:3000/'); | |
} | |
}; | |
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); | |
} | |
}); | |
}); | |
}, | |
disconnect: function(){ | |
socket.disconnect(); | |
} | |
}; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment