Skip to content
Permalink
Browse files

application Node.js server

  • Loading branch information...
nickholub committed Jan 20, 2014
1 parent 8ec3779 commit 224e9918e5b71c1ca97ca6ff31c2335ba1fa50ee
Showing with 197 additions and 11 deletions.
  1. +1 −0 .jshintrc
  2. +5 −0 Gruntfile.js
  3. +8 −8 app.js
  4. +6 −0 app/index.html
  5. +11 −1 app/scripts/app.js
  6. +43 −0 app/scripts/controllers/serverdata.js
  7. +111 −0 app/scripts/services/websocket.js
  8. +5 −0 app/settings.js
  9. +2 −1 bower.json
  10. +4 −1 config.js
  11. +1 −0 karma.conf.js
@@ -20,6 +20,7 @@
"smarttabs": true,
"globals": {
"angular": false,
"jQuery": false,
"_": false,
"d3": false
}
@@ -262,6 +262,11 @@ module.exports = function (grunt) {
cwd: '<%= yeoman.app %>/template',
dest: '<%= yeoman.dist %>/template',
src: '**/*.html'
}, {
expand: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: 'settings.js'
}]
},
styles: {
16 app.js
@@ -45,15 +45,15 @@ function createBroadcast(topic) {

function broadcast() {
randomValue = nextValue(randomValue);
var data = { value: randomValue };
var msgObject = { topic: topic, data: data};
var msg = JSON.stringify(msgObject);

for (var key in clients) {
if(clients.hasOwnProperty(key)) {
clients[key].write(msg);
}
var data = { value: randomValue };
var msgObject = { topic: topic, data: data};
var msg = JSON.stringify(msgObject);

for (var key in clients) {
if (clients.hasOwnProperty(key)) {
clients[key].write(msg);
}
}
}

return broadcast;
@@ -22,6 +22,8 @@

<div class="container-full" ng-view=""></div>

<script src="settings.js"></script>

<!-- build:js scripts/modules.js -->
<script src="bower_components/jquery/jquery.js"></script>
<script src="bower_components/underscore/underscore.js"></script>
@@ -38,11 +40,14 @@
<script src="bower_components/nvd3/nv.d3.js"></script>
<script src="bower_components/angularjs-nvd3-directives/dist/angularjs-nvd3-directives.js"></script>
<script src="bower_components/angular-ui-dashboard/dist/angular-ui-dashboard.js"></script>
<script src="bower_components/pines-notify/jquery.pnotify.js"></script>
<script src="bower_components/angular-pines-notify/src/pnotify.js"></script>
<!-- endbuild -->

<!-- build:js({.tmp,app}) scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script src="scripts/services/service.js"></script>
<script src="scripts/services/websocket.js"></script>
<script src="scripts/widgets/time/time.js"></script>
<script src="scripts/widgets/random/random.js"></script>
<script src="scripts/widgets/scopeWatch/scopeWatch.js"></script>
@@ -52,6 +57,7 @@
<script src="scripts/widgets/lineChart/lineChart.js"></script>
<script src="scripts/widgets/pieChart/pieChart.js"></script>
<script src="scripts/controllers/main.js"></script>
<script src="scripts/controllers/serverdata.js"></script>
<!-- endbuild -->

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
@@ -4,6 +4,7 @@ angular.module('ui.dashboard.widgets', ['ngGrid']);

angular.module('app', [
'app.service',
'app.websocket',
'ngRoute',
'ngCookies',
'ngResource',
@@ -12,12 +13,21 @@ angular.module('app', [
'nvd3ChartDirectives',
'ui.dashboard.widgets'
])
.config(function ($routeProvider) {
.constant('settings', window.settings)
.config(function ($routeProvider, webSocketProvider, settings) {
if (settings) {
webSocketProvider.setWebSocketURL(settings.webSocketURL);
}

$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/serverdata', {
templateUrl: 'views/main.html',
controller: 'ServerDataCtrl'
})
.otherwise({
redirectTo: '/'
});
@@ -0,0 +1,43 @@
'use strict';

angular.module('app')
.controller('ServerDataCtrl', function ($scope, webSocket, settings) {
var widgetDefinitions = [
{
name: 'wt-scope-watch',
attrs: {
value: 'serverValue'
}
},
{
name: 'wt-top-n',
attrs: {
data: 'serverTopTen'
}
}
];

var defaultWidgets = _.clone(widgetDefinitions);

$scope.dashboardOptions = {
widgetButtons: true,
widgetDefinitions: widgetDefinitions,
defaultWidgets: defaultWidgets
};

$scope.serverValue = 0;
webSocket.subscribe(settings.randomValueTopic, function (message) {
$scope.serverValue = message.value;
$scope.$apply();
}, $scope);

webSocket.subscribe(settings.topNTopic, function (message) {
var list = [];
_.each(message, function(value, key) {
list.push( { name: key, value: parseInt(value, 10) } );
});

$scope.serverTopTen = list;
$scope.$apply();
}, $scope);
});
@@ -0,0 +1,111 @@
'use strict';

angular.module('app.websocket', ['ui.notify'])
.provider('webSocket', function () {

var webSocketURL;
var webSocketObject; // for testing only

return {
$get: function ($q, $rootScope, notificationService) {
if (!webSocketURL && !webSocketObject) {
throw 'WebSocket URL is not defined';
}

var socket = !webSocketObject ? new WebSocket(webSocketURL) : webSocketObject;

var deferred = $q.defer();

socket.onopen = function () {
deferred.resolve();
$rootScope.$apply();
};

var webSocketError = false;

socket.onclose = function () {
if (!webSocketError) {
notificationService.notify({
title: 'WebSocket Closed',
text: 'WebSocket connection has been closed. Try refreshing the page.',
type: 'error',
icon: false,
hide: false,
history: false
});
}
};

//TODO
socket.onerror = function () {
webSocketError = true;
notificationService.notify({
title: 'WebSocket Error',
text: 'WebSocket error. Try refreshing the page.',
type: 'error',
icon: false,
hide: false,
history: false
});
};

var topicMap = {}; // topic -> [callbacks] mapping

socket.onmessage = function (event) {
var message = JSON.parse(event.data);

var topic = message.topic;

if (topicMap.hasOwnProperty(topic)) {
topicMap[topic].fire(message.data);
}
};

return {
send: function (message) {
var msg = JSON.stringify(message);

deferred.promise.then(function () {
console.log('send ' + msg);
socket.send(msg);
});
},

subscribe: function (topic, callback, $scope) {
var callbacks = topicMap[topic];

if (!callbacks) {
var message = { type: 'subscribe', topic: topic }; // subscribe message
this.send(message);

callbacks = jQuery.Callbacks();
topicMap[topic] = callbacks;
}

callbacks.add(callback);

if ($scope) {
$scope.$on('$destroy', function () {
this.unsubscribe(topic, callback);
}.bind(this));
}
},

unsubscribe: function (topic, callback) {
if (topicMap.hasOwnProperty(topic)) {
var callbacks = topicMap[topic];
callbacks.remove(callback);
}
}
};
},

setWebSocketURL: function (wsURL) {
webSocketURL = wsURL;
},

setWebSocketObject: function (wsObject) {
webSocketObject = wsObject;
}
};
});
@@ -0,0 +1,5 @@
// client-side settings (for dev only)
window.settings = {};
settings.webSocketURL = 'ws://node1.morado.com:9090/pubsub';
settings.randomValueTopic = 'randomValue';
settings.topNTopic = 'demos.twitter.topURLs';
@@ -20,7 +20,8 @@
"ng-grid": "~2.0.7",
"d3": "~3.3.13",
"nvd3": "~1.1.15-beta",
"angularjs-nvd3-directives": "~0.0.5-beta"
"angularjs-nvd3-directives": "~0.0.5-beta",
"angular-pines-notify": "~0.0.3"
},
"devDependencies": {
"angular-mocks": "~1.0.7",
@@ -4,6 +4,9 @@ config.port = process.env.PORT || 3000;

// client settings (passed to the browser)
config.settings = {};
config.settings.webSocketURL = 'ws://localhost/sockjs/websocket';
var settings = config.settings;
settings.webSocketURL = 'ws://localhost:3000/sockjs/websocket';
settings.randomValueTopic = 'randomValue';
settings.topNTopic = 'demos.twitter.topURLs';

module.exports = config;
@@ -24,6 +24,7 @@ module.exports = function(config) {
'app/bower_components/angular-ui-dashboard/src/angular-ui-dashboard.js',
'app/bower_components/ng-grid/ng-grid-2.0.7.debug.js',
'app/bower_components/angularjs-nvd3-directives/dist/angularjs-nvd3-directives.js',
'app/bower_components/angular-pines-notify/src/pnotify.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',

0 comments on commit 224e991

Please sign in to comment.
You can’t perform that action at this time.