-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketapi.js
150 lines (138 loc) · 4.56 KB
/
socketapi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const io = require("socket.io")();
const socketapi = {
io: io
};
// boards: holds board objects
// for now, board is a list of data for drawing objects
// board: [
// {
// type: "Pen",
// data: {path, size, color}
// }
// {
// type: "Rectangle"
// data: {...}
// }
// ]
let boards = {};
let nextIds = {};
function validateCode(code, socket) {
if (boards[code]) {
return false;
}
socket.emit("begone");
return true;
}
// socket io logic goes here
io.on("connection", (socket) => {
// join
// Broadcast to client the board when they join so they can get it
// and draw it for the first time
socket.on("join", function (data) {
let code = data.code;
// join room for code
socket.join(code);
// create board if non-existent already
if (!(code in boards)) {
boards[code] = {};
nextIds[code] = 0;
}
nextIds[code]++;
// send board and next id back to user
socket.emit("joinData", { nextId: nextIds[code], board: boards[code] });
});
// update
// Broadcast client updating objects
// data:
// {
// code: the code for the board being drawn on
// id: the id of the pen object
// size: the stroke-width of the pen object
// color: the color of the pen object
// newPoints: the points being added to the Pen object's path
// path: updated path for server to use
// text: the new text in the textObject
// }
socket.on("update", function (data) {
if(validateCode(data.code, socket)){
return;
}
socket.to(data.code).emit('update', data);
boards[data.code][data.id].data.content = data.content;
});
// add
// Add a new object to the saved board for sending to client on join
// data:
// {
// code: the code for the board being drawn on
// id: the id of the pen object
// size: the stroke-width of the pen object / the font size
// color: the color of the pen object / the font color
// text: the text in the textObject
// }
socket.on("add", function (data) {
if(validateCode(data.code, socket)){
return;
}
socket.to(data.code).emit('add', data);
boards[data.code][data.id] = { type: data.type, isEditing:data.isEditing, data: { content: data.content, size: data.size, color: data.color, x: data.x, y: data.y } };
});
socket.on("updatePosition", function (data) {
if(validateCode(data.code, socket)){
return;
}
boards[data.code][data.id].position = data.position;
boards[data.code][data.id].data.content.upperLeft = data.upperLeft;
boards[data.code][data.id].data.content.lowerRight = data.lowerRight;
socket.to(data.code).emit("updatePosition", data);
});
socket.on('erase', function (data) {
if(validateCode(data.code, socket)){
return;
}
delete boards[data.code][data.id];
socket.to(data.code).emit('erase', data);
});
socket.on("multiErase", function(data) {
if(validateCode(data.code, socket)){
return;
}
for(let i = 0; i < data.ids.length; i++) {
delete boards[data.code][data.ids[i]];
socket.to(data.code).emit("erase", {id: data.ids[i]})
}
});
// requestNewId
// Get a new ID for the client to attach to a new drawing object, done
// on server side so that each user's drawings increments the nextId.
// data:
// {
// code: the code for the board being drawn on
// }
socket.on("requestNewId", function (data) {
if(validateCode(data.code, socket)){
return;
}
nextIds[data.code]++;
if(boards[data.code][data.id]){
boards[data.code][data.id].isEditing = false;
}
socket.emit("newId", { newId: nextIds[data.code]});
socket.to(data.code).emit("completeEdit", {id:data.id});
});
// clearBoard
// Broadcast to the clients to clear the board
// data:
// {
// code: the code for the board being cleared
// }
socket.on("clearBoard", function (data) {
let code = data.code;
if(validateCode(code, socket)){
return;
}
socket.to(code).emit('clearBoard');
boards[code] = {};
});
});
module.exports = socketapi;