Last active
April 1, 2017 19:51
-
-
Save znechai/d29670680404e7b2b43f6de9e04c1b86 to your computer and use it in GitHub Desktop.
Angularjs - Factory for Socket.io
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
function socket($rootScope) { | |
var socket = io.connect('https://HOST:PORT'); | |
return { | |
emit: function (eventName, data, callback) { | |
socket.emit(eventName, data, function () { | |
var args = arguments; | |
$rootScope.$apply(function () { | |
if (callback) { | |
callback.apply(socket, args); | |
} | |
}); | |
}); | |
}, | |
on: function (eventName, callback) { | |
socket.on(eventName, function () { | |
var args = arguments; | |
$rootScope.$apply(function () { | |
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