-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmediengewitter.js
77 lines (63 loc) · 1.93 KB
/
mediengewitter.js
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
/*!
* mediengewitter.js is the server component of mediengewitter
*
* It combines connect middleware
* with a websocket-server which is able to push pictures
* to our clients.
*
* @author pfleidi
* @author makefu
*
*/
var Connect = require('connect'),
Fs = require('fs'),
Log4js = require('log4js'),
Io = require('socket.io'),
Http = require('http'),
PORT = 8080,
WEBROOT = __dirname + '/static',
LOGFILE = __dirname + '/log/mediengewitter.log';
Log4js.loadAppender('file');
Log4js.addAppender(Log4js.appenders.file(LOGFILE), 'mediengewitter');
var logger = Log4js.getLogger('mediengewitter');
logger.setLevel('DEBUG');
//TODO make options changeable via commandline params
var IMAGE_PATH = "static/content/",
NEW_IMAGES_FILE = IMAGE_PATH + "imageSum",
imageCache = require('./lib/imagecache').createCache(NEW_IMAGES_FILE, logger),
DELAY = 7500;
var logStream = Fs.createWriteStream(__dirname + '/log/access.log', {
encoding: 'utf-8',
flags: 'a'
});
var app = Connect();
var httpServer = Http.createServer(app);
[
Connect.static(WEBROOT),
Connect.compress(),
Connect.logger(),
Connect.errorHandler({ showStack: true, dumpExceptions: true})
].forEach(function (middleware) {
app.use(middleware);
});
httpServer.listen(PORT, function () {
logger.debug('Webserver successfully started.');
});
var webSocketServer = Io.listen(httpServer, {
flashPolicyServer: false,
log: function () { logger.debug }
});
webSocketServer.on('connection', function (connection) {
var cache = imageCache.cache();
connection.send(JSON.stringify(cache));
connection.on('message', function(message) {
log.info('Got msg: ' + message);
});
});
(function doAction() {
var currImage = imageCache.nextImage();
logger.info("Current Image is : " + currImage);
var toSend = JSON.stringify({ data: 'content/' + currImage });
webSocketServer.sockets.send(toSend);
setTimeout(doAction, DELAY);
}());