-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
61 lines (46 loc) · 1.6 KB
/
server.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
const express = require('express'),
http = require('http'),
urlencoded = require('body-parser').urlencoded,
socketIo = require('socket.io'),
twilioRouter = require('./twilioRouter') ;
require('dotenv').config();
const app = express();
app.use(express.static(__dirname + '/static'));
app.use(urlencoded({ extended: false }));
app.use(twilioRouter);
const server = http.createServer(app)
const port = process.env.PORT || 3000;
console.log(`Twilio Client app HTTP server running at http://localhost:${port}`);
server.listen(port);
var io = socketIo.listen(server);
let { getElementHistory, insertDB, updateElDB, deleteLastElDB } = require( './canvasLib')
// event-handler for new incoming connections
io.on('connection', function (socket) {
getElementHistory().then( (el_history) => {
for (let objD of el_history) {
if (objD !== '') {
if (objD.type === 'polygon') {
socket.emit('draw_poly', objD)
} else {
socket.emit('draw_line', objD);
}
}
}
})
socket.on('start_line', (objD) => {
insertDB(objD);
socket.broadcast.emit('start_line', objD);
})
socket.on('real_time_line', (objD) => {
updateElDB(objD);
socket.broadcast.emit('real_time_line', objD);
});
socket.on('undo', (projectId) => {
deleteLastElDB(projectId);
io.emit('undo', (projectId));
})
socket.on('draw_poly', (objD) => {
insertDB(objD);
socket.broadcast.emit('draw_poly', objD);
})
});