so://phie

outline

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

blob: e8f46958a1597527236b93ce2867e6a8dd270a03 (2393 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: 'server.typable.dev',

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

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

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

32});

33

34server.path('/pipe');

35server.listen(proxy);

36

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

38const paint = require('./mod/paint');

39

40let client = {};

41let width = 1920;

42let height = 1080;

43let canvas = createCanvas(width, height);

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

45g.fillStyle = 'white';

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

47

48paint.init(g);

49

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

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

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

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

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

55 });

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

57 paint.draw(data);

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

59 });

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

61 g.fillStyle = 'white';

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

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

64 });

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

66 g.fillStyle = data;

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

68 g.fillStyle = 'black';

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

70 });

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

72 if(typeof client[socket.id] === 'undefined') {

73 client[socket.id] = data.uuid;

74 }

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

76 });

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

78 if(typeof client[socket.id] !== 'undefined') {

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

80 quit: true,

81 uuid: client[socket.id]

82 });

83 delete client[socket.id];

84 }

85 });

86});

87

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

89 if(typeof client[socket.id] !== 'undefined') {

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

91 quit: true,

92 uuid: client[socket.id].uuid

93 });

94 delete client[socket.id];

95 }

96});