-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (31 loc) · 769 Bytes
/
index.js
File metadata and controls
35 lines (31 loc) · 769 Bytes
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
var express = require('express');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var lines = [];
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + "/index.html");
});
io.on('connection', (socket) => {
console.log('A client connected');
drawAllLines(socket);
socket.on('disconnect', () => {
console.log('Client disconnected');
});
socket.on('draw', (line) => {
lines.push(line);
io.emit('draw', line);
});
socket.on('redraw', (line) => {
drawAllLines(socket);
});
});
function drawAllLines(socket) {
lines.forEach(line => {
socket.emit('draw', line);
});
}
http.listen(3000, () => {
console.log('Listening on port 3000!');
});