so://phie

outline

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

blob: 85c920e6b5f7d6db5b82d872b31e0307cb370a2b (1829 bytes)[doc][blame][raw]

1const express = require('express');

2const app = express();

3const http = require('http').createServer(app);

4const io = require('socket.io')(http);

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

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

7

8let port = 80;

9let client = {};

10

11let width = 1920;

12let height = 1080;

13let canvas = createCanvas(width, height);

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

15g.fillStyle = 'white';

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

17

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

19

20const proxy = http.listen(port, function() {

21 console.log('Server started on port: ' + port);

22});

23

24paint.init(g);

25

26io.path('/pipe');

27io.listen(proxy);

28

29io.on('connection', function(socket) {

30 client[socket] = {};

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

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

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

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

35 });

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

37 paint.draw(data);

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

39 });

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

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

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

43 });

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

45 g.fillStyle = data;

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

47 g.fillStyle = 'black';

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

49 });

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

51 if(!client[socket]) {

52 client[socket] = {};

53 }

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

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

56 });

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

58 if(client[socket]) {

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

60 quit: true,

61 uuid: client[socket].uuid

62 });

63 }

64 delete client[socket];

65 });

66});

67io.on('disconnect', function(socket) {

68 if(client[socket]) {

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

70 quit: true,

71 uuid: client[socket].uuid

72 });

73 delete client[socket];

74 }

75});