-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
32 lines (24 loc) · 924 Bytes
/
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
const path = require('path');
const express = require('express');
const app = express();
const socketio = require('socket.io');
// app.listen() returns an http.Server object
// http://expressjs.com/en/4x/api.html#app.listen
const server = app.listen(1337, function () {
console.log(`Listening on http://localhost:${server.address().port}`);
});
const io = socketio(server);
const inMemoryDrawHistory = [];
io.on('connection', socket => {
console.log('A new client has connected!');
console.log(socket.id);
if (inMemoryDrawHistory.length) socket.emit('load', inMemoryDrawHistory);
socket.on('draw', function (start, end, color) {
inMemoryDrawHistory.push({ start, end, color });
socket.broadcast.emit('someOneDrew', start, end, color);
});
socket.on('disconnect', function () {
console.log('Goodbye, ', socket.id, ' :(');
});
});
app.use(express.static(path.join(__dirname, 'public')));