-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.js
48 lines (39 loc) · 1.19 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
/**
* Created by bhavyaagg on 11/01/18.
*/
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;
const utils = require('./utils');
const currentSessionsArray = [];
app.use(express.static(__dirname + '/public'));
function onConnection(socket) {
socket.on('drawing', (data) => {
socket.broadcast.emit('drawing', data)
});
socket.on('drawingInSession', (data) => {
socket.broadcast.to(data.sessionId).emit('drawingInSession', data);
});
socket.on("createSession", () => {
let newSessionId = utils.randomSessionId(currentSessionsArray);
currentSessionsArray.push(newSessionId);
socket.join(newSessionId);
socket.emit("createdSession", {
success: true,
sessionId: newSessionId
});
});
socket.on("joinSession", (data) => {
if (currentSessionsArray.includes(data.sessionId)) {
socket.join(data.sessionId);
socket.emit("joinedSession", {
success: true,
sessionId: data.sessionId
})
}
})
}
io.on('connection', onConnection);
http.listen(port, () => console.log('listening on port ' + port));