2012-06-05 08:45:50 +03:00
|
|
|
var express = require('express'),
|
2012-06-05 08:54:10 +03:00
|
|
|
app = express.createServer(express.logger()),
|
2012-06-05 08:45:50 +03:00
|
|
|
io = require('socket.io').listen(app),
|
|
|
|
routes = require('./routes');
|
|
|
|
|
|
|
|
// Configuration
|
|
|
|
|
|
|
|
app.configure(function() {
|
|
|
|
app.set('views', __dirname + '/views');
|
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
app.use(express.bodyParser());
|
|
|
|
app.use(express.methodOverride());
|
|
|
|
app.use(app.router);
|
|
|
|
app.use(express.static(__dirname + '/public'));
|
|
|
|
});
|
|
|
|
|
|
|
|
app.configure('development', function() {
|
|
|
|
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
|
|
|
|
});
|
|
|
|
|
|
|
|
app.configure('production', function() {
|
|
|
|
app.use(express.errorHandler());
|
|
|
|
});
|
|
|
|
|
2012-06-05 09:57:20 +03:00
|
|
|
// Heroku won't actually allow us to use WebSockets
|
|
|
|
// so we have to setup polling instead.
|
|
|
|
// https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku
|
|
|
|
// io.configure(function () {
|
|
|
|
// io.set("transports", ["xhr-polling"]);
|
|
|
|
// io.set("polling duration", 10);
|
|
|
|
// });
|
2012-06-05 08:54:10 +03:00
|
|
|
|
2012-06-05 08:45:50 +03:00
|
|
|
// Routes
|
|
|
|
|
2012-06-05 09:57:20 +03:00
|
|
|
var port = process.env.PORT || 5000; // Use the port that Heroku provides or default to 5000
|
2012-06-05 08:54:10 +03:00
|
|
|
app.listen(port, function() {
|
2012-06-05 08:45:50 +03:00
|
|
|
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/', routes.index);
|
|
|
|
|
|
|
|
var status = "All is well.";
|
|
|
|
|
|
|
|
io.sockets.on('connection', function (socket) {
|
2012-06-05 09:57:20 +03:00
|
|
|
io.sockets.emit('status', { status: status }); // note the use of io.sockets to emit but socket.on to listen
|
2012-06-05 08:45:50 +03:00
|
|
|
socket.on('reset', function (data) {
|
|
|
|
status = "War is imminent!";
|
2012-06-05 09:57:20 +03:00
|
|
|
io.sockets.emit('status', { status: status });
|
2012-06-05 08:45:50 +03:00
|
|
|
});
|
|
|
|
});
|