From 5e755a04d1190697b8940fe8262f49683d686da1 Mon Sep 17 00:00:00 2001 From: Dominic Bosch Date: Tue, 24 Mar 2015 23:35:54 +0100 Subject: [PATCH] Angular and Websocket awesomeness --- client/index.html | 8 +++++--- client/js/app.js | 2 +- client/js/controllers.js | 20 +++++++++++++++++++- npm-debug.log | 24 ------------------------ server/server.js | 15 +++++++++++++-- 5 files changed, 38 insertions(+), 31 deletions(-) delete mode 100644 npm-debug.log diff --git a/client/index.html b/client/index.html index 106e48b..a34fb7b 100644 --- a/client/index.html +++ b/client/index.html @@ -3,15 +3,17 @@ Awesome - + + \ No newline at end of file diff --git a/client/js/app.js b/client/js/app.js index 3762422..9a93874 100644 --- a/client/js/app.js +++ b/client/js/app.js @@ -1,3 +1,3 @@ -var app = angular.module( 'blapp' ); +var app = angular.module( 'blapp', [] ); diff --git a/client/js/controllers.js b/client/js/controllers.js index f7bef8c..7442501 100644 --- a/client/js/controllers.js +++ b/client/js/controllers.js @@ -1,6 +1,12 @@ -var blapp = angular.module( 'blapp', [] ); +var blapp = angular.module( 'blapp' ); blapp.controller( 'blappCtrl', function ($scope) { + socket.on( 'ratingupdate', function (data) { + console.log(data); + $scope.$apply( function() { + $scope.bleus.push(data); + }); + }); $scope.bleus = [ { 'name': 'Restaurant Sonne', @@ -11,4 +17,16 @@ blapp.controller( 'blappCtrl', function ($scope) { 'rating': 3.4 } ]; + $scope.sendToServer = function( data ) { + socket.emit('newrating', data ); + console.log( 'wuah!', data ); + }; +}); + + + +var socket = io.connect('http://localhost:8080'); +socket.on('news', function (data) { + console.log(data); + socket.emit('my other event', { my: 'data' }); }); \ No newline at end of file diff --git a/npm-debug.log b/npm-debug.log deleted file mode 100644 index 4664927..0000000 --- a/npm-debug.log +++ /dev/null @@ -1,24 +0,0 @@ -0 info it worked if it ends with ok -1 verbose cli [ 'node', '/usr/local/bin/npm', 'start' ] -2 info using npm@2.1.12 -3 info using node@v0.10.25 -4 verbose node symlink /usr/bin/node -5 verbose stack Error: missing script: start -5 verbose stack at run (/usr/local/lib/node_modules/npm/lib/run-script.js:130:19) -5 verbose stack at /usr/local/lib/node_modules/npm/lib/run-script.js:81:5 -5 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:51:40 -5 verbose stack at final (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:349:17) -5 verbose stack at then (/usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:126:33) -5 verbose stack at /usr/local/lib/node_modules/npm/node_modules/read-package-json/read-json.js:331:40 -5 verbose stack at evalmachine.:268:14 -5 verbose stack at /usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:102:5 -5 verbose stack at Object.oncomplete (evalmachine.:107:15) -6 verbose cwd /home/dominic/projects/bleuguide -7 error Linux 3.13.0-46-generic -8 error argv "node" "/usr/local/bin/npm" "start" -9 error node v0.10.25 -10 error npm v2.1.12 -11 error missing script: start -12 error If you need help, you may report this error at: -12 error -13 verbose exit [ 1, true ] diff --git a/server/server.js b/server/server.js index 39720e0..76286fb 100644 --- a/server/server.js +++ b/server/server.js @@ -1,4 +1,5 @@ -var express = require( 'express' ); +var express = require( 'express' ), + socketio = require('socket.io'); app = express(); app.get( '/', function( req, res ) { @@ -10,5 +11,15 @@ app.use( express.static( './client' ) ); exports.init = function( port ) { console.log( 'Starting to listen on port ' + port ); - app.listen( port ); + var server = app.listen( port ); + var io = socketio( server ); + + io.on('connection', function (socket) { + socket.emit('news', { hello: 'world' }); + socket.on('newrating', function (data) { + console.log(data); + data.rating += 1; + socket.emit( 'ratingupdate', data ); + }); + }); }; \ No newline at end of file