so://phie

outline

A web-based drawing app
git clone git://soophie.de/outline

blob: 94229630a1e390f17421640df8885afab9fc4160 (2390 bytes)[doc][blame][raw]

1const fs = require('fs');

2const express = require('express');

3const app = express();

4const http = require('http');

5const https = require('https');

6const io = require('socket.io');

7

8let env = {

9 port: 443,

10 path: '/etc/letsencrypt/live/',

11 domain: 'prototype-studio.de',

12 ssl: true

13};

14

15if(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}

22else {

23 env.ssl = false;

24 env.port = 80;

25}

26

27const proxy = env.ssl ? https.createServer(env.credentials, app) : http.createServer(app);

28const server = io(https);

29

30app.use(express.static(__dirname + '/public'));

31

32proxy.listen(env.port, function() {

33 console.log('Server started on port:', env.port);

34});

35

36server.path('/pipe');

37server.listen(proxy);

38

39const { createCanvas } = require('canvas');

40const paint = require('./public/js/mod/paint');

41

42let client = {};

43let width = 1920;

44let height = 1080;

45let canvas = createCanvas(width, height);

46let g = canvas.getContext('2d');

47g.fillStyle = 'white';

48g.fillRect(0, 0, width, height);

49

50paint.init(g);

51

52server.on('connection', function(socket) {

53 client[socket] = {};

54 socket.on('request', function(data) {

55 let { x, y, width, height } = data;

56 let image = g.getImageData(x, y, width, height);

57 socket.emit('load', new Uint8Array(image.data).buffer);

58 });

59 socket.on('data' , function(data) {

60 paint.draw(data);

61 socket.broadcast.emit('data', data);

62 });

63 socket.on('clear' , function(data) {

64 g.clearRect(0, 0, width, height);

65 socket.broadcast.emit('clear', data);

66 });

67 socket.on('background' , function(data) {

68 g.fillStyle = data;

69 g.fillRect(0, 0, width, height);

70 g.fillStyle = 'black';

71 socket.broadcast.emit('background', data);

72 });

73 socket.on('cursor' , function(data) {

74 if(!client[socket]) {

75 client[socket] = {};

76 }

77 client[socket].uuid = data.uuid;

78 socket.broadcast.emit('cursor', data);

79 });

80 socket.on('disconnect', function() {

81 if(client[socket]) {

82 socket.broadcast.emit('cursor', {

83 quit: true,

84 uuid: client[socket].uuid

85 });

86 }

87 delete client[socket];

88 });

89});

90server.on('disconnect', function(socket) {

91 if(client[socket]) {

92 socket.broadcast.emit('cursor', {

93 quit: true,

94 uuid: client[socket].uuid

95 });

96 delete client[socket];

97 }

98});