-
-
Save runeh/5135316 to your computer and use it in GitHub Desktop.
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
app.controller( 'AppCtrl', function ($scope, socket) { | |
socket.onopen( | |
function(){ | |
console.log('Socket is connected :D') | |
} | |
) | |
socket.onclose( | |
function(){ | |
console.log('Socket is disconnected :(') | |
} | |
) | |
}) |
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
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