-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (33 loc) · 1015 Bytes
/
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
var path = require('path');
var http = require('http');
var server = http.createServer();
var express = require('express');
var app = express();
var socketio = require('socket.io');
server.on('request', app);
var io = socketio(server);
var inMemoryDrawHistory = [];
const port = process.env.PORT || 1337;
console.log('port:', port)
server.listen(port, function () {
console.log('The server is listening on port 3000!');
});
app.use(express.static(path.join(__dirname, 'browser')));
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
io.on('connection', function (socket) {
console.log(socket.id, 'connected');
socket.emit('load', inMemoryDrawHistory);
socket.on('draw', function (start, end, color) {
inMemoryDrawHistory.push({
start: start,
end: end,
color: color
});
socket.broadcast.emit('draw', start, end, color);
});
socket.on('disconnect', function () {
console.log('Goodbye, ', socket.id, ' :(');
});
});