Move control socket

This commit is contained in:
Albert Stefanov 2023-10-26 08:29:45 +03:00
parent 48701890df
commit bc8b805b2f
1 changed files with 39 additions and 38 deletions

77
app.js
View File

@ -1,4 +1,5 @@
var express = require('express'), const
express = require('express'),
app = module.exports = express(), app = module.exports = express(),
server = require('http').createServer(app), server = require('http').createServer(app),
Stopwatch = require('./models/stopwatch'), Stopwatch = require('./models/stopwatch'),
@ -28,41 +29,43 @@ var port = process.env.PORT || 5000;
var host = process.env.HOST || '0.0.0.0'; var host = process.env.HOST || '0.0.0.0';
server.listen(port, host, function() { server.listen(port, host, function() {
console.log("Express server listening on %j in %s mode", server.address(), app.settings.env); console.log("Express server listening on %j in %s mode", server.address(), app.settings.env);
}); });
var stopwatch = new Stopwatch(); var stopwatch = new Stopwatch();
stopwatch.on('tick:stopwatch', function(time) { stopwatch.on('tick:stopwatch', function(time) {
io.sockets.emit('time', { time: time }); io.sockets.emit('time', { time: time });
}); });
stopwatch.on('reset:stopwatch', function(time) { stopwatch.on('reset:stopwatch', function(time) {
io.sockets.emit('time', { time: time }); io.sockets.emit('time', { time: time });
}); });
stopwatch.start(); stopwatch.start();
io.sockets.on('connection', function (socket) { io.sockets.on('connection', function (socket) {
io.sockets.emit('time', { time: stopwatch.getTime() }); io.sockets.emit('time', { time: stopwatch.getTime() });
socket.on('click:start', function () { socket.on('click:start', function () {
stopwatch.start(); stopwatch.start();
}); });
socket.on('click:stop', function () { socket.on('click:stop', function () {
stopwatch.stop(); stopwatch.stop();
}); });
socket.on('click:zero', function () { socket.on('click:zero', function () {
stopwatch.zero(); stopwatch.zero();
}); });
socket.on('click:reset', function () { socket.on('click:reset', function () {
stopwatch.reset(); stopwatch.reset();
}); });
socket.on('click:resetShort', function () { socket.on('click:resetShort', function () {
stopwatch.resetShort(); stopwatch.resetShort();
}); });
}); });
@ -75,43 +78,41 @@ app.get('/', function(req, res) {
res.render('index', { title: process.env.TITLE }); res.render('index', { title: process.env.TITLE });
}); });
app.get('/control/', function(req, res) {
const control = express.Router();
control.get('/', function(req, res) {
res.render('control', { title: process.env.TITLE }); res.render('control', { title: process.env.TITLE });
}); });
control.post('/reset/', function (req, res) {
app.post('/reset/', function (req, res) {
stopwatch.reset(); stopwatch.reset();
res.send("OK"); res.send("OK");
}); });
control.post('/reset-short/', function (req, res) {
app.post('/reset-short/', function (req, res) {
stopwatch.resetShort(); stopwatch.resetShort();
res.send("OK"); res.send("OK");
}); });
control.post('/start-from-reset/', function (req, res) {
app.post('/start-from-reset/', function (req, res) {
stopwatch.reset(); stopwatch.reset();
stopwatch.start(); stopwatch.start();
res.send("OK"); res.send("OK");
}); });
control.post('/start-from-reset-short/', function (req, res) {
app.post('/start-from-reset-short/', function (req, res) {
stopwatch.resetShort(); stopwatch.resetShort();
stopwatch.start(); stopwatch.start();
res.send("OK"); res.send("OK");
}); });
control.post('/start/', function (req, res) {
app.post('/start/', function (req, res) {
stopwatch.start(); stopwatch.start();
res.send("OK"); res.send("OK");
}); });
control.post('/stop/', function (req, res) {
app.post('/stop/', function (req, res) {
stopwatch.stop(); stopwatch.stop();
res.send("OK"); res.send("OK");
}); });
control.post('/zero/', function (req, res) {
app.post('/zero/', function (req, res) {
stopwatch.zero(); stopwatch.zero();
res.send("OK"); res.send("OK");
}); });
app.use('/control', control);