-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
312 lines (245 loc) · 11.5 KB
/
app.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const path = require('path');
const mysql = require('mysql');
const port = 8080;
var board = [];
var cnt = 0;
var connections = 0;
// connect to the database with a pool for effeciency
var pool = mysql.createPool({
connectionLimit: process.env.MYSQL_CONNECT_LIMIT,
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
});
app.get('*', function(req, res) {
// req.url
res.sendFile(path.join(__dirname + '/public/index.html'))
});
io.on('connection', function(socket) {
console.log('user connected--------------------------------', socket.handshake.headers.referer, socket.id);
//http://paperjs.org/reference/pathitem/#datas
let re = new RegExp(/\/([a-zA-Z0-9]+)/);
// result[1] should be the part after http://url/boards/<this_part>
let result = socket.handshake.query.room.match(re);
if (result == null) {
console.log("no match on register");
return;
}
let thisBoard = result[1];
// console.log("thisBoard", thisBoard);
// we have a specifc board, subscribe the user to the room
socket.join(thisBoard, function() {
console.log("in thisBoard", thisBoard)
console.log("socket is in rooms", socket.rooms);
// get a connection to the DB and see if the board exists
pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!
console.log("connections", ++connections);
// INSERT UNLESS KEY EXIST:
// Use the connection
console.log("abotu to select")
connection.query("SELECT * FROM boards LEFT JOIN paths on boards.name = paths.board WHERE boards.name = " + mysql.escape(thisBoard), function(error, results, fields) {
console.log("in seelct")
// check for error
if (error) {
// all done, release this connection
connection.release();
console.log("a connection released err", --connections);
throw error;
}
// no results, insert the board into the DB
if (results.length == 0) {
connection.query("INSERT INTO boards (name, owner) VALUES (" + mysql.escape(thisBoard) + ", " + mysql.escape(socket.id) + ")", function(error, results, fields) {
if (error) throw error;
console.log("SQL INSERT RESULT?", results);
// tell myself the current state of the room from the server
socket.emit('log', "joined on insert " + thisBoard);
// all done, release this connection
connection.release();
console.log("connection released", --connections);
});
} else {
// the board did exist, got the results
console.log("SQL QUERY RESULT?", results);
// tell myself the current state of the room from the server
socket.emit('log', "joined on query (existed) " + thisBoard);
socket.emit('boardState', results);
// all done, release this connection
connection.release();
console.log("connection released", --connections);
}
});
});
});
socket.on('disconnect', function() {
// users are automatically removed from the room when they disconnect
console.log('user disconnected');
});
socket.on('startDraw', function(path, cb) {
// let p = JSON.parse(path);
// console.log("start DRAW ->>>>", p, path[0].segments);
// board[childrenIndex] = { path: path, points: [p[1].segments[0]] };
// cache.set(thisBoard, board);
// socket.to(thisBoard).emit('log', cache.get("thisBoard"));
path = JSON.parse(path);
// path[1].segments = [];
console.log("THIS PATH", path);
insertPath(path,cb);
});
function insertPath(path, cb) {
// insert/get next ID from DB
pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!
console.log("connection acquired", ++connections);
// start transaction (get last insert for this board)
connection.beginTransaction(function(error) {
// check for error
if (error) {
// all done, release this connection
connection.release();
console.log("connection released", --connections);
throw error;
}
connection.query("SELECT MAX(idx) AS idx FROM paths WHERE board = ?", [thisBoard], function(error, qresults, fields) {
// check for error, cancel the transaction
if (error) {
return connection.rollback(function(){
connection.release();
console.log("connection released err", --connections);
throw error;
}
)}
socket.emit('log', qresults);
var idx;
// handle first insert
if (qresults == null || qresults[0].idx == null) {
idx = -1;
console.log("!!ALERT!!! idx is null");
} else if (qresults[0].idx > -1) {
idx = qresults[0].idx;
console.log("--> qresult fine, is", idx);
}
console.log("** IDX is now", idx);
// insert the current point
// console.log(" ATTEMPTING INSERT", thisBoard, (idx), path);
// console.log('INSERT INTO paths (board, idx, json_string) VALUES (' + mysql.escape(thisBoard) + ', ' + mysql.escape((idx + 1)) + ', ' + mysql.escape(JSON.stringify(path)) + ')');
connection.query('INSERT INTO paths (board, idx, json_string) VALUES (?, ?, ?)', [thisBoard, (idx + 1), JSON.stringify(path)], function(error, iresults, fields) {
// check for error, cancel the transaction
if (error) {
return connection.rollback(
function(){
connection.release();
console.log("connection released err", --connections);
throw error;
}
)
}
connection.commit(function(error){
// check for error, cancel the transaction
if (error) {
return connection.rollback(function(){
connection.release();
console.log("connection released err", --connections);
throw error;
}
)}
// successful
// let the front end know what index to use
cb(idx + 1);
console.log("Next index returned ", idx + 1);
console.log("StartDraw insert transaction successful at ", (idx + 1));
socket.emit('log', "StartDraw insert transaction successful: " + (idx + 1));
socket.to(thisBoard).emit('startDraw', (idx + 1), path);
connection.release();
// all done, release this connection
console.log("connection released", --connections);
});
});
});
});
});
}
socket.on('updateDraw', function(childrenIndex, xy) {
// console.log("update DRAW", xy, board);
// board[childrenIndex].points.push(xy);
// cache.set(thisBoard, board);
// socket.to(thisBoard).emit('log', cache.get("thisBoard"));
console.log("updateDraw " + childrenIndex + " **");
socket.to(thisBoard).emit('updateDraw', childrenIndex, xy);
});
socket.on('endDraw', function(index, simplifiedPath, erase = false) {
if (erase) {
console.log("endDraw but really endErase " + index);
} else {
console.log("endDraw " + index);
}
// get a connection to the DB and see if the board exists
pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!
// all done, release this connection
console.log("connection acquired", ++connections);
// UPDATE THE JSON
// overwrite the previous json since line points will be simplified
connection.query("UPDATE paths SET json_string = ? WHERE board = ? AND idx = ?", [JSON.stringify(simplifiedPath), thisBoard, index], function(error, results, fields) {
if (error) {
return connection.rollback(function(){
connection.release();
console.log("connection released err", --connections);
throw error;
}
)}
// handle erasing
if (erase) {
socket.to(thisBoard).emit('endErase', index);
} else {
socket.to(thisBoard).emit('endDraw', index);
}
// all done, release this connection
connection.release();
console.log("connection released", --connections);
});
});
});
socket.on('undo', function(index, cb) {
console.log("undo");
// insert/get next ID from DB
pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!
console.log("connection acquired", ++connections);
connection.query("DELETE FROM paths WHERE board = ? AND idx = ?", [thisBoard, index], function(error, qresults, fields) {
// check for error, cancel the transaction
if (error) {
return connection.rollback(
function(){
connection.release();
console.log("connection released err", --connections);
cb(false);
throw error;
}
)
}
cb(true);
socket.to(thisBoard).emit('undo', index);
connection.release();
});
});
});
socket.on('redo', function(index) {
console.log("redo");
socket.to(thisBoard).emit('redo', index);
});
socket.on('pan', function(x, y) {
console.log("pan");
socket.to(thisBoard).emit('pan', x, y);
});
socket.on('error', function(error) {
console.log("ERROR", error);
});
});
http.listen(port, function() {
console.log('listening on *:' + port);
});