-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
114 lines (89 loc) · 3.38 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import express from "express";
import cors from "cors";
import * as http from "http";
import { Server } from "socket.io";
import dotenv from "dotenv";
import morgan from "morgan";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 8080;
// Log the middleware
app.use(morgan('combined'));
app.use(cors({
origin: process.env.ALLOWED_ORIGINS ? process.env.ALLOWED_ORIGINS.split(',') : '*',
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type']
}));
const server = http.createServer(app);
const io = new Server(server, { cors: { origin: "*", methods: ["GET", "POST"] } });
const rooms = new Map();
const handleJoinRoom = (socket, { room_id, name }) => {
socket.join(room_id);
if (!rooms.has(room_id)) {
rooms.set(room_id, new Map);
};
const room = rooms.get(room_id);
room.set(socket.id, name);
socket.emit("add-existing-names", Array.from(room).map(([id, name]) => ({ id, name })));
socket.to(room_id).emit("add-new-name", { id: socket.id, name });
socket.emit("existing-cursor", Array.from(room).map(id => ({ id })));
socket.to(room_id).emit("new-cursor", { id: socket.id });
console.log(`User ${socket.id} joined room ${room_id}`);
};
const handleDisconnect = (socket) => {
rooms.forEach((room, room_id) => {
if (room.has(socket.id)) {
room.delete(socket.id);
io.to(room_id).emit("remove-cursor", socket.id)
if (room.size === 0) {
rooms.delete(room_id);
console.log(`Room ${room_id} has been removed`);
}
}
})
console.log(`User disconnected: ${socket.id}`);
}
io.on("connection", (socket) => {
console.log(`[connection] connected with user: ${socket.id}`);
socket.on("joinRoom", (data) => handleJoinRoom(socket, data));
socket.on("mouse-move", (data) => {
const { room_id, x, y, scrollx, scrolly } = data;
socket.to(room_id).emit("mouse-move", { socket_id: socket.id, x, y, scrollx, scrolly });
})
socket.on("new-block-added", (data) => {
const { room_id, update } = data;
socket.to(room_id).emit("new-block-added", update);
console.log(`User ${socket.id} sent the changes in room ${room_id}`);
})
socket.on("new-block-deleted", (data) => {
const { room_id, update } = data;
socket.to(room_id).emit("new-block-deleted", update);
console.log(`User ${socket.id} deleted the block in room ${room_id}`);
})
socket.on("block-moved/connected/disconnected", (data) => {
const { room_id, update } = data;
socket.to(room_id).emit("block-moved/connected/disconnected", update);
console.log(`User ${socket.id} changed the block in room ${room_id}`);
})
socket.on("block-value-updated", (data) => {
const { room_id, update } = data;
socket.to(room_id).emit("block-value-updated", update);
console.log(`User ${socket.id} changed the value of a block in room ${room_id}`);
})
socket.on("exit-collaboration", ({ room_id }) => {
if (rooms.has(room_id)) {
const room = rooms.get(room_id);
const usersArray = Array.from(room).map(([id, name]) => (id));
socket.emit("exit-collaboration", usersArray);
console.log(`User ${socket.id} exited the collaboration`);
}
})
socket.on("disconnect", () => handleDisconnect(socket));
});
// Simple test endpoint
app.get("/test", (req, res) => {
res.json({ ok: true });
});
server.listen(PORT, () => {
console.log(`Server's up and running on port ${PORT} `);
});