This repository was archived by the owner on Jun 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
82 lines (73 loc) · 2.42 KB
/
Copy pathserver.js
File metadata and controls
82 lines (73 loc) · 2.42 KB
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
import { createServer } from "http";
import next from "next";
import { Server } from "socket.io";
import { pool } from "./lib/pool.js";
import { decrypt } from "./lib/utils.js";
const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = 3000;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handler = app.getRequestHandler();
app.prepare().then(() => {
const httpServer = createServer(handler);
const io = new Server(httpServer, { perMessageDeflate: false });
io.on("connection", (socket) => {
const info = socket.handshake.auth;
const { workSpaceID, userID } = {
workSpaceID: decrypt(info.workSpaceID).replace(/^"|"$/g, ""),
userID: decrypt(info.userID).replace(/^"|"$/g, ""),
};
socket.join(workSpaceID);
socket.on("ready", async () => {
try {
const sql = " SELECT task_id, is_done, updated_by_user, updated_at "
+ " FROM progresses "
+ " WHERE workspace_id = ? "
+ " ORDER BY task_id ASC; "
const [progress,] = await pool.execute(sql, [workSpaceID]);
io.to(socket.id).emit("progress", progress);
} catch (err) {
console.error(err);
return;
}
});
socket.on("taskupdate", async (id, checked) => {
// emit to all clients in the same room
try {
const sql =
"INSERT INTO progresses (workspace_id, task_id, is_done, updated_by_user) " +
"VALUES (?, ?, ?, ?) " +
"ON DUPLICATE KEY UPDATE is_done = VALUES(is_done), updated_by_user = VALUES(updated_by_user);";
await pool.execute(sql, [workSpaceID, id, checked, userID]);
} catch (err) {
console.error(err);
return;
}
io.to(workSpaceID).emit(
"taskupdated",
{
id,
taskInfo: { task_id: id, is_done: checked, updated_by_user: userID, updated_at: new Date() }
});
});
});
httpServer
.once("error", (err) => {
console.error(err);
process.exit(1);
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
});
process.on('SIGINT', async () => {
console.log('Received SIGINT. Shutting down gracefully.');
await pool.end();
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('Received SIGTERM. Shutting down gracefully.');
await pool.end();
process.exit(0);
});