Last active
August 29, 2015 14:02
-
-
Save ben-bradley/0cc04f3f7aec75946a1e to your computer and use it in GitHub Desktop.
angular socketio factory
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
'use strict'; | |
angular | |
.module('myApp', [ | |
'ngCookies', | |
'ngResource', | |
'ngSanitize', | |
'ngRoute' | |
]) | |
.config(function ($routeProvider) { | |
$routeProvider | |
.when('/', { | |
templateUrl: 'views/main.html', | |
controller: 'MainCtrl' | |
}) | |
.otherwise({ | |
redirectTo: '/' | |
}); | |
}); |
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
angular.module('myApp') | |
.controller('MainCtrl', function ($scope, socket) { | |
$scope.time = 'loading...'; | |
socket.on('time', function(ms) { | |
$scope.time = new Date(ms).toLocaleTimeString(); | |
}); | |
}); |
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
angular.module('myApp') | |
.factory('socket', function($rootScope) { | |
var socket = io.connect('http://localhost:8000'); | |
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); | |
}); | |
}); | |
} | |
}; | |
}) |
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
<!doctype html> | |
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> | |
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> | |
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> | |
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> | |
<head> | |
<meta charset="utf-8"> | |
<title></title> | |
<meta name="description" content=""> | |
<meta name="viewport" content="width=device-width"> | |
<!-- Place favicon.ico and apple-touch-icon.png in the root directory --> | |
<!-- build:css styles/vendor.css --> | |
<!-- bower:css --> | |
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" /> | |
<!-- endbower --> | |
<!-- endbuild --> | |
<!-- build:css({.tmp,app}) styles/main.css --> | |
<link rel="stylesheet" href="styles/main.css"> | |
<!-- endbuild --> | |
</head> | |
<body ng-app="myApp"> | |
<!--[if lt IE 7]> | |
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> | |
<![endif]--> | |
<!-- Add your site or application content here --> | |
<div class="container" ng-view=""></div> | |
<!--[if lt IE 9]> | |
<script src="bower_components/es5-shim/es5-shim.js"></script> | |
<script src="bower_components/json3/lib/json3.min.js"></script> | |
<![endif]--> | |
<!-- build:js scripts/vendor.js --> | |
<!-- bower:js --> | |
<script src="bower_components/jquery/dist/jquery.js"></script> | |
<script src="bower_components/angular/angular.js"></script> | |
<script src="bower_components/bootstrap/dist/js/bootstrap.js"></script> | |
<script src="bower_components/angular-resource/angular-resource.js"></script> | |
<script src="bower_components/angular-cookies/angular-cookies.js"></script> | |
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script> | |
<script src="bower_components/angular-route/angular-route.js"></script> | |
<!-- endbower --> | |
<!-- endbuild --> | |
<!-- build:js({.tmp,app}) scripts/scripts.js --> | |
<script src='socket.io/socket.io.js'></script> | |
<script src="scripts/app.js"></script> | |
<script src="scripts/controllers/socketio.controller.js"></script> | |
<script src='scripts/factories/socketio.factory.js'></script> | |
<!-- endbuild --> | |
</body> | |
</html> |
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
<h1>SocketIO Server Time: {{time}}</h1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment