Skip to content

Instantly share code, notes, and snippets.

@runeh
Forked from lrvick/controllers.js
Created March 11, 2013 16:04
Show Gist options
  • Save runeh/5135316 to your computer and use it in GitHub Desktop.
Save runeh/5135316 to your computer and use it in GitHub Desktop.
app.controller( 'AppCtrl', function ($scope, socket) {
socket.onopen(
function(){
console.log('Socket is connected :D')
}
)
socket.onclose(
function(){
console.log('Socket is disconnected :(')
}
)
})
app.value('version', '0.1').factory( 'socket' , function($rootScope) {
var port = (location.port != 80) ? ':'+location.port : ''
var socket = new WebSocket('ws://'+document.domain+''+port+'/socket')
var methods =
{ onopen:
function(callback) {
socket.onopen = function(){
var args = arguments;
$rootScope.$apply(function(){ callback.apply(socket,args) })
}
}
, onmessage:
function(data, callback) {
socket.onmessage = function(data){
var args = arguments;
$rootScope.$apply(function(){ callback.apply(socket,args) })
}
}
, onclose:
function(callback) {
socket.onclose = function(){
var args = arguments;
$rootScope.$apply(function(){ callback.apply(socket,args) })
}
}
}
return methods
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment