-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (63 loc) · 1.88 KB
/
server.js
File metadata and controls
73 lines (63 loc) · 1.88 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
import express from 'express';
import { createServer } from 'node:http';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { Server } from 'socket.io';
const app = express();
const server = createServer(app);
const io = new Server(server);
app.use(express.static('dist'));
app.use('/dist', express.static('assets'));
const __dirname = dirname(fileURLToPath(import.meta.url));
function defineStatus(n) {
const status = {
1: 'Esperando acesso',
2: 'Timer rodando',
3: 'Pronto para iniciar'
}
console.log(status[n])
io.emit('status', status[n])
}
app.get('/', (req, res) => {
res.sendFile(join(__dirname, '/dist/index.html'));
})
io.on('connection', (socket) => {
console.log('usuário conectado');
socket.on('login', (username) => {
socket.on('status', (status) => {
if (status == 'Esperando acesso' && username == 'admin') {
defineStatus(3);
} else if (status == 'Pronto para iniciar' && username == 'admin') {
}
else {
console.log('status inválido\n' + status)
}
});
socket.on('start', _ => {
socket.on('statusReturn', (statusReturn) => {
if (username == 'admin' && statusReturn === 'Pronto para iniciar') {
console.log('timer iniciado');
let ocounter = 14;
const counter = setInterval(() => {
defineStatus(2);
io.emit('timer', ocounter.toString());
ocounter--;
if (ocounter < 1) {
clearInterval(counter);
io.emit('timer', ocounter = 15);
defineStatus(1);
}
}, 1000);
} else {
console.log("farei nada não")
}
})
})
});
socket.on('disconnect', () => {
console.log('usuário desconectado');
});
});
server.listen(3000, () => {
console.log('server rodando na famosa porta 3000');
})