-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (45 loc) · 1.39 KB
/
index.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
52
53
54
55
56
57
58
const express = require('express');
const app = express();
const ejs = require('ejs');
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const port = process.env.PORT || 3000;
actionList = []
io.on('connection', (socket) => {
console.log('User online: ' + socket.id);
actionList.forEach((element) => {
if (element === 'undo-command') {
socket.emit('undo-command');
} else if (element === 'redo-command') {
socket.emit('redo-command');
} else {
socket.emit('draw-command', element);
}
});
socket.on('draw-command', (data) => {
actionList.push(data);
socket.broadcast.emit('draw-command', data);
});
socket.on('undo-command', () => {
actionList.push('undo-command');
socket.broadcast.emit('undo-command');
});
socket.on('redo-command', () => {
actionList.push('redo-command');
socket.broadcast.emit('redo-command');
});
socket.on('clear-canvas', () => {
actionList = [];
socket.broadcast.emit('clear-canvas');
});
socket.on('message', (data) => {
socket.broadcast.emit('message', data);
});
});
server.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('main.ejs');
});