|
|
@@ -5,7 +5,9 @@ var iolib = require('socket.io')
|
|
5
|
5
|
var MAX_EMIT_COUNT = 64; // Maximum number of draw operations before getting banned
|
|
6
|
6
|
var MAX_EMIT_COUNT_PERIOD = 5000; // Duration (in ms) after which the emit count is reset
|
|
7
|
7
|
|
|
8
|
|
-// Map from name to *promises* of BoardData
|
|
|
8
|
+/** Map from name to *promises* of BoardData
|
|
|
9
|
+ @type {Object<string, Promise<BoardData>>}
|
|
|
10
|
+*/
|
|
9
|
11
|
var boards = {};
|
|
10
|
12
|
|
|
11
|
13
|
function noFail(fn) {
|
|
|
@@ -24,7 +26,9 @@ function startIO(app) {
|
|
24
|
26
|
return io;
|
|
25
|
27
|
}
|
|
26
|
28
|
|
|
27
|
|
-/** Returns a promise to a BoardData with the given name*/
|
|
|
29
|
+/** Returns a promise to a BoardData with the given name
|
|
|
30
|
+ * @returns {Promise<BoardData>}
|
|
|
31
|
+*/
|
|
28
|
32
|
function getBoard(name) {
|
|
29
|
33
|
if (boards.hasOwnProperty(name)) {
|
|
30
|
34
|
return boards[name];
|
|
|
@@ -37,30 +41,28 @@ function getBoard(name) {
|
|
37
|
41
|
|
|
38
|
42
|
function socketConnection(socket) {
|
|
39
|
43
|
|
|
40
|
|
- function joinBoard(name) {
|
|
|
44
|
+ async function joinBoard(name) {
|
|
41
|
45
|
// Default to the public board
|
|
42
|
46
|
if (!name) name = "anonymous";
|
|
43
|
47
|
|
|
44
|
48
|
// Join the board
|
|
45
|
49
|
socket.join(name);
|
|
46
|
50
|
|
|
47
|
|
- return getBoard(name).then(board => {
|
|
48
|
|
- board.users.add(socket.id);
|
|
49
|
|
- log('board joined', { 'board': board.name, 'users': board.users.size });
|
|
50
|
|
- return board;
|
|
51
|
|
- });
|
|
|
51
|
+ var board = await getBoard(name);
|
|
|
52
|
+ board.users.add(socket.id);
|
|
|
53
|
+ log('board joined', { 'board': board.name, 'users': board.users.size });
|
|
|
54
|
+ return board;
|
|
52
|
55
|
}
|
|
53
|
56
|
|
|
54
|
57
|
socket.on("error", noFail(function onError(error) {
|
|
55
|
58
|
log("ERROR", error);
|
|
56
|
59
|
}));
|
|
57
|
60
|
|
|
58
|
|
- socket.on("getboard", noFail(function onGetBoard(name) {
|
|
59
|
|
- joinBoard(name).then(board => {
|
|
60
|
|
- //Send all the board's data as soon as it's loaded
|
|
61
|
|
- socket.emit("broadcast", { _children: board.getAll() });
|
|
62
|
|
- });
|
|
63
|
|
- }));
|
|
|
61
|
+ socket.on("getboard", async function onGetBoard(name) {
|
|
|
62
|
+ var board = await joinBoard(name);
|
|
|
63
|
+ //Send all the board's data as soon as it's loaded
|
|
|
64
|
+ socket.emit("broadcast", { _children: board.getAll() });
|
|
|
65
|
+ });
|
|
64
|
66
|
|
|
65
|
67
|
socket.on("joinboard", noFail(joinBoard));
|
|
66
|
68
|
|
|
|
@@ -102,41 +104,39 @@ function socketConnection(socket) {
|
|
102
|
104
|
}));
|
|
103
|
105
|
|
|
104
|
106
|
socket.on('disconnecting', function onDisconnecting(reason) {
|
|
105
|
|
- Object.keys(socket.rooms).forEach(function disconnectFrom(room) {
|
|
|
107
|
+ Object.keys(socket.rooms).forEach(async function disconnectFrom(room) {
|
|
106
|
108
|
if (boards.hasOwnProperty(room)) {
|
|
107
|
|
- boards[room].then(board => {
|
|
108
|
|
- board.users.delete(socket.id);
|
|
109
|
|
- var userCount = board.users.size;
|
|
110
|
|
- log('disconnection', { 'board': board.name, 'users': board.users.size });
|
|
111
|
|
- if (userCount === 0) {
|
|
112
|
|
- board.save();
|
|
113
|
|
- delete boards[room];
|
|
114
|
|
- }
|
|
115
|
|
- });
|
|
|
109
|
+ var board = await boards[room];
|
|
|
110
|
+ board.users.delete(socket.id);
|
|
|
111
|
+ var userCount = board.users.size;
|
|
|
112
|
+ log('disconnection', { 'board': board.name, 'users': board.users.size });
|
|
|
113
|
+ if (userCount === 0) {
|
|
|
114
|
+ board.save();
|
|
|
115
|
+ delete boards[room];
|
|
|
116
|
+ }
|
|
116
|
117
|
}
|
|
117
|
118
|
});
|
|
118
|
119
|
});
|
|
119
|
120
|
}
|
|
120
|
121
|
|
|
121
|
|
-function saveHistory(boardName, message) {
|
|
|
122
|
+async function saveHistory(boardName, message) {
|
|
122
|
123
|
var id = message.id;
|
|
123
|
|
- getBoard(boardName).then(board => {
|
|
124
|
|
- switch (message.type) {
|
|
125
|
|
- case "delete":
|
|
126
|
|
- if (id) board.delete(id);
|
|
127
|
|
- break;
|
|
128
|
|
- case "update":
|
|
129
|
|
- delete message.type;
|
|
130
|
|
- if (id) board.update(id, message);
|
|
131
|
|
- break;
|
|
132
|
|
- case "child":
|
|
133
|
|
- board.addChild(message.parent, message);
|
|
134
|
|
- break;
|
|
135
|
|
- default: //Add data
|
|
136
|
|
- if (!id) throw new Error("Invalid message: ", message);
|
|
137
|
|
- board.set(id, message);
|
|
138
|
|
- }
|
|
139
|
|
- });
|
|
|
124
|
+ var board = await getBoard(boardName);
|
|
|
125
|
+ switch (message.type) {
|
|
|
126
|
+ case "delete":
|
|
|
127
|
+ if (id) board.delete(id);
|
|
|
128
|
+ break;
|
|
|
129
|
+ case "update":
|
|
|
130
|
+ delete message.type;
|
|
|
131
|
+ if (id) board.update(id, message);
|
|
|
132
|
+ break;
|
|
|
133
|
+ case "child":
|
|
|
134
|
+ board.addChild(message.parent, message);
|
|
|
135
|
+ break;
|
|
|
136
|
+ default: //Add data
|
|
|
137
|
+ if (!id) throw new Error("Invalid message: ", message);
|
|
|
138
|
+ board.set(id, message);
|
|
|
139
|
+ }
|
|
140
|
140
|
}
|
|
141
|
141
|
|
|
142
|
142
|
function generateUID(prefix, suffix) {
|