-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (33 loc) · 1.13 KB
/
server.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
const port = 8033;
const express = require('express');
const app = express();
const httpServer = require('http').createServer(app);
const {Server} = require('socket.io');
const io = new Server(httpServer, {
cors: {
origin: true,
},
});
app.use(express.static('public'));
console.log('starting socket server on :' + port);
httpServer.listen(port);
let clientIdIncrement = 0;
// Memorize the date so we can re-broadcast it when a client joins
let timerEndDate = null;
io.on('connection', function (socket) {
const clientName = '[socket.io#' + (++clientIdIncrement) + '@' + socket.request.socket.remoteAddress + '] ';
console.log(clientName + 'connected');
socket.emit('countDownTo', timerEndDate);
socket.on('countDownTo', async date => {
console.log(clientName + 'count down to ' + date);
timerEndDate = date;
io.emit('countDownTo', timerEndDate);
});
socket.on('ping', async data => {
console.log(clientName + 'ping');
io.emit('ping', data);
});
socket.on('disconnect', function () {
console.log(clientName + 'disconnected');
});
});