Created
March 1, 2015 22:07
-
-
Save huttj/e52a3851248356198389 to your computer and use it in GitHub Desktop.
Basic socket usage in Angular/Ionic
This file contains 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
angular.module('app') | |
.service('Socket', function($rootScope) { | |
var socket = io.connect('http://www.foo.com:5000'); | |
var S = {}; | |
S.on = function(eventName, callback) { | |
socket.on(eventName, function () { | |
var args = arguments; | |
$rootScope.$apply(function () { | |
callback.apply(null, args); | |
}); | |
}); | |
} | |
S.emit = function(eventName, data, callback) { | |
socket.emit(eventName, data, function () { | |
var args = arguments; | |
if (callback) { | |
$rootScope.$apply(function () { | |
callback.apply(null, args); | |
}); | |
} | |
}); | |
} | |
return S; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment