This repository was archived by the owner on May 30, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
100 lines (80 loc) · 2.27 KB
/
Copy pathapp.js
File metadata and controls
100 lines (80 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*!
* app.js
*
* @author pfleidi
*/
var Fs = require('fs');
var Util = require('util');
var Express = require('express');
var Io = require('socket.io');
var Log4js = require('log4js')();
var logger = Log4js.getLogger('bunghole');
var LOGFILE = __dirname + '/logs/bunghole.log';
// main application
var app = module.exports = Express.createServer();
/**
* configuration
*/
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(Express.bodyParser());
app.use(app.router);
app.use(Express.static(__dirname + '/public'));
});
app.configure('development', function () {
app.use(Express.errorHandler({
dumpExceptions: true,
showStack: true
}));
logger.setLevel('DEBUG');
});
app.configure('production', function () {
var accessLog = Fs.createWriteStream(__dirname + '/logs/access.log', {
encoding: 'utf-8',
flags: 'a'
});
app.use(Express.logger({ stream: accessLog }));
app.use(Express.errorHandler());
Log4js.addAppender(Log4js.fileAppender(LOGFILE));
logger.setLevel('ERROR');
});
app.listen(3000, function () {
var adr = app.address();
Util.puts('Express server listening on http://' + adr.address + ':' + adr.port + '/');
});
var webSocketServer = Io.listen(app, {
flashPolicyServer: false
});
webSocketServer.on('connection', function (connection) {
function _dispatch(msg, connection) {
console.dir(msg);
}
connection.on('message', function (message) {
try {
var msg = JSON.parse(message);
_dispatch(msg, connection);
} catch (err) {
log.error('Couldn\'t parse or eval message: ' + err.stack);
}
});
});
var plotSin = [];
var plotCos = [];
var plot = [plotSin, plotCos];
var count = 0;
(function broadcast() {
count += 0.1;
plotSin.push([count, Math.sin(count)]);
plotCos.push([count, Math.cos(count)]);
webSocketServer.broadcast(JSON.stringify(plot));
setTimeout(broadcast, 200);
}());
/* Process Logging */
process.on('SIGINT', function () {
logger.info('Got SIGINT. Exiting ...');
process.exit(0);
});
process.on('uncaughtException', function (err) {
logger.fatal('RUNTIME ERROR! :' + err.stack);
});