-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
37 lines (31 loc) · 1.03 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
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const Canvas = require('canvas');
const drawing = require('./drawing');
const util = require('util');
const canvas = Canvas.createCanvas(1024, 600);
const ctx = canvas.getContext('2d');
ctx.strokeStyle = "rgb(0, 0, 0)";
ctx.lineCap = "round";
const port = parseInt(process.argv[2] || '3000');
const debug = process.argv[3] == 'true';
app.use(express.static('public'));
app.get('/drawing.js', function(req, res) {
res.sendFile('drawing.js', { root: __dirname });
});
io.on('connection', function(socket) {
console.log('New connection');
socket.emit('state', canvas.toDataURL());
socket.on('draw', function(data) {
if (debug) {
console.log(util.inspect(data, {showHidden: false, depth: null}));
}
drawing.draw.list(ctx, data);
socket.broadcast.emit('draw', data);
});
});
http.listen(port, function() {
console.log('listening on *:' + port);
});