-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (50 loc) · 1.66 KB
/
index.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
import express from 'express';
import http from 'http';
import { Server } from "socket.io";
import { fileURLToPath } from 'url';
import Client from './mongo/mongo.js';
const __dirname = fileURLToPath(import.meta.url).replace('index.js', '');
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: "*",
}
});
var client = new Client();
await client.Connect();
app.get('/test', (req, res) => {
res.sendFile(__dirname + '/static/index.html');
});
io.on('connection', (socket) => {
// join a room, and create a new sketch
// and send the sketch id to the client
socket.on('join', (sketch_id) => {
(async () => {
let sketch = await client.GetSketch(sketch_id);
if (sketch == null) {
console.log("Creating new sketch");
sketch_id = await client.NewSketch(sketch_id);
console.log("New sketch id: " + sketch_id)
}
socket.join(sketch_id);
console.log("Joined sketch: " + sketch_id);
// emit the sketch to only the client that just joined
socket.emit('sketch', sketch);
})();
});
socket.on('line', (sketch_id, line) => {
console.log("Line: " + line
+ " from sketch: " + sketch_id)
// add the line to the database
client.NewLine(sketch_id, line);
socket.broadcast.to(sketch_id).emit('sync', line);
});
socket.on('clear', (sketch_id) => {
client.ClearLines(sketch_id);
io.to(sketch_id).emit('clear');
});
});
server.listen(3000, () => {
console.log('listening on *:3000');
});