A web-based drawing app
git clone git://soophie.de/outline
blob: 77a9e9634b57a9e867e9a9d17137c61b80ab32ff (2354 bytes)[doc][blame][raw]
1
const fs = require('fs');2
const express = require('express');3
const app = express();4
const http = require('http');5
const https = require('https');6
const io = require('socket.io');8
let env = {9
port: 443,10
path: '/etc/letsencrypt/live/',11
domain: 'server.typable.dev',12
ssl: true13
};15
if(fs.existsSync(env.path)) {16
env.credentials = {17
key: fs.readFileSync(env.path + env.domain + '/privkey.pem', 'utf8'),18
cert: fs.readFileSync(env.path + env.domain + '/cert.pem', 'utf8'),19
ca: fs.readFileSync(env.path + env.domain + '/chain.pem', 'utf8')20
};21
}22
else {23
env.ssl = false;24
env.port = 80;25
}27
const proxy = env.ssl ? https.createServer(env.credentials, app) : http.createServer(app);28
const server = io(https);30
proxy.listen(env.port, function() {31
console.log('Server started on port:', env.port);32
});34
server.path('/pipe');35
server.listen(proxy);37
const { createCanvas } = require('canvas');38
const paint = require('./mod/paint');40
let client = {};41
let width = 1920;42
let height = 1080;43
let canvas = createCanvas(width, height);44
let g = canvas.getContext('2d');45
g.fillStyle = 'white';46
g.fillRect(0, 0, width, height);48
paint.init(g);50
server.on('connection', function(socket) {51
client[socket] = {};52
socket.on('request', function(data) {53
let { x, y, width, height } = data;54
let image = g.getImageData(x, y, width, height);55
socket.emit('load', new Uint8Array(image.data).buffer);56
});57
socket.on('data' , function(data) {58
paint.draw(data);59
socket.broadcast.emit('data', data);60
});61
socket.on('clear' , function(data) {62
g.fillStyle = 'white';63
g.fillRect(0, 0, width, height);64
socket.broadcast.emit('clear', data);65
});66
socket.on('background' , function(data) {67
g.fillStyle = data;68
g.fillRect(0, 0, width, height);69
g.fillStyle = 'black';70
socket.broadcast.emit('background', data);71
});72
socket.on('cursor' , function(data) {73
if(!client[socket]) {74
client[socket] = {};75
}76
client[socket].uuid = data.uuid;77
socket.broadcast.emit('cursor', data);78
});79
socket.on('disconnect', function() {80
if(client[socket]) {81
socket.broadcast.emit('cursor', {82
quit: true,83
uuid: client[socket].uuid84
});85
}86
delete client[socket];87
});88
});89
server.on('disconnect', function(socket) {90
if(client[socket]) {91
socket.broadcast.emit('cursor', {92
quit: true,93
uuid: client[socket].uuid94
});95
delete client[socket];96
}97
});