2012-06-05 08:45:50 +03:00
|
|
|
var express = require('express'),
|
2012-06-08 17:06:54 +03:00
|
|
|
app = module.exports = express.createServer(express.logger()),
|
|
|
|
io = require('socket.io').listen(app);
|
|
|
|
Stopwatch = require('./models/stopwatch'),
|
2012-06-05 08:45:50 +03:00
|
|
|
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
|
2012-06-08 17:06:54 +03:00
|
|
|
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-08 17:06:54 +03:00
|
|
|
// Use the port that Heroku provides or default to 5000
|
|
|
|
var port = process.env.PORT || 5000;
|
2022-10-12 22:53:16 +03:00
|
|
|
var host = process.env.HOST || '0.0.0.0';
|
|
|
|
app.listen(port, host, function() {
|
|
|
|
console.log("Express server listening on %j in %s mode", app.address(), app.settings.env);
|
2012-06-05 08:45:50 +03:00
|
|
|
});
|
|
|
|
|
2022-10-12 22:53:16 +03:00
|
|
|
// configure title
|
|
|
|
app.title = process.env.TITLE || 'Timer'
|
|
|
|
|
2012-06-05 08:45:50 +03:00
|
|
|
app.get('/', routes.index);
|
|
|
|
|
2012-06-08 17:06:54 +03:00
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
stopwatch.on('tick:stopwatch', function(time) {
|
|
|
|
io.sockets.emit('time', { time: time });
|
|
|
|
});
|
|
|
|
|
|
|
|
stopwatch.on('reset:stopwatch', function(time) {
|
|
|
|
io.sockets.emit('time', { time: time });
|
|
|
|
});
|
|
|
|
|
|
|
|
stopwatch.start();
|
2012-06-05 08:45:50 +03:00
|
|
|
|
|
|
|
io.sockets.on('connection', function (socket) {
|
2012-06-08 17:06:54 +03:00
|
|
|
io.sockets.emit('time', { time: stopwatch.getTime() });
|
|
|
|
|
|
|
|
socket.on('click:start', function () {
|
|
|
|
stopwatch.start();
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('click:stop', function () {
|
|
|
|
stopwatch.stop();
|
|
|
|
});
|
|
|
|
|
2022-10-12 22:53:16 +03:00
|
|
|
socket.on('click:zero', function () {
|
|
|
|
stopwatch.zero();
|
|
|
|
});
|
|
|
|
|
2012-06-08 17:06:54 +03:00
|
|
|
socket.on('click:reset', function () {
|
|
|
|
stopwatch.reset();
|
2012-06-05 08:45:50 +03:00
|
|
|
});
|
2022-10-12 22:53:16 +03:00
|
|
|
|
|
|
|
socket.on('click:resetShort', function () {
|
|
|
|
stopwatch.resetShort();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/reset/', function (req, res) {
|
|
|
|
stopwatch.reset();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
app.post('/reset-short/', function (req, res) {
|
|
|
|
stopwatch.resetShort();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
app.post('/start-from-reset/', function (req, res) {
|
|
|
|
stopwatch.reset();
|
|
|
|
stopwatch.start();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
app.post('/start-from-reset-short/', function (req, res) {
|
|
|
|
stopwatch.resetShort();
|
|
|
|
stopwatch.start();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
app.post('/start/', function (req, res) {
|
|
|
|
stopwatch.start();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
app.post('/stop/', function (req, res) {
|
|
|
|
stopwatch.stop();
|
|
|
|
res.send("OK");
|
|
|
|
});
|
|
|
|
app.post('/zero/', function (req, res) {
|
|
|
|
stopwatch.zero();
|
|
|
|
res.send("OK");
|
|
|
|
});
|