-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (63 loc) · 2.04 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const mongoose = require("mongoose");
const { faker } = require('@faker-js/faker');
const ui = require("./routes/ui");
const api = require("./routes/api");
const RetroSpec = require("./model/RetroPoint");
const app = express();
app.use("/ui", ui);
app.use("/api", api);
app.get('/', (req, res) => {
res.redirect(302, '/ui')
})
const server = http.createServer(app);
const io = socketIo(server);
mongoose.connect("mongodb://localhost:27017/retrospec");
io.on('connection', (socket) => {
socket.data.username = faker.internet.userName();
console.log(`New client connected! Id : ${socket.id}`);
socket.join(socket.id);
socket.on('message', (message) => {
io.emit('message', message);
})
socket.on('typing', (e) => {
const typingUsers = {username : socket.data.username};
io.emit('typing', typingUsers)
})
socket.on('stop_typing', (e) => {
const typingUsers = {username : socket.data.username};
io.emit('stop_typing', typingUsers)
})
socket.on('disconnect', (e) => {
console.log(`Client Disconnected!\nReason : ${e}`);
})
socket.on('move', async (data) => {
if(data) {
const point = await RetroSpec.findById(data.id);
point.bucket = data.targetId;
await point.save();
}
io.emit('move', data);
});
socket.on("delete", async (data) => {
if(data) {
const msg = await RetroSpec.findById(data);
msg.deleted = true;
await msg.save();
}
});
socket.on("save", async (data) => {
const msg = new RetroSpec({
_id: data._id,
message: data.message,
bucket: data.bucket,
dashboardId: data.dashboardId,
createdTime: data.createdTime
});
await msg.save();
})
});
const port = 4201;
server.listen(port, '0.0.0.0', () => console.log(`Listening on port ${port}`));