Skip to content

Commit 8b6f7fc

Browse files
committed
Added cursor position streaming and better ux all over
1 parent 2a6940e commit 8b6f7fc

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

apps/http-server/dist/controllers/roomControllers.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
1212
exports.createRoomController = createRoomController;
1313
exports.joinRoomController = joinRoomController;
1414
exports.fetchAllRoomsController = fetchAllRoomsController;
15+
exports.fetchRoomByIdController = fetchRoomByIdController;
1516
const client_1 = require("@workspace/db/client");
1617
const utils_1 = require("../utils");
1718
const common_1 = require("@workspace/common");
@@ -178,3 +179,33 @@ function fetchAllRoomsController(req, res) {
178179
}
179180
});
180181
}
182+
function fetchRoomByIdController(req, res) {
183+
return __awaiter(this, void 0, void 0, function* () {
184+
const userId = req.userId;
185+
const roomId = req.params.roomId;
186+
if (!userId) {
187+
res.status(401).json({ message: "User Id not found" });
188+
return;
189+
}
190+
if (!roomId) {
191+
res.status(400).json({ message: "Room Id required" });
192+
return;
193+
}
194+
try {
195+
const existingRooms = yield client_1.db.select().from(client_1.roomsTable).where((0, drizzle_orm_1.eq)(client_1.roomsTable.id, roomId));
196+
const room = existingRooms[0];
197+
if (!room) {
198+
res.status(404).json({ message: "Room not found" });
199+
return;
200+
}
201+
res.json({
202+
message: "Room fetched successfully",
203+
room,
204+
});
205+
}
206+
catch (e) {
207+
console.log(e);
208+
res.status(500).json({ message: "Error fetching room" });
209+
}
210+
});
211+
}

apps/http-server/dist/routes/roomRouter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ const router = (0, express_1.Router)();
77
router.route("/create").post(authenticateUser_1.authenticateUser, roomControllers_1.createRoomController);
88
router.route("/join").post(authenticateUser_1.authenticateUser, roomControllers_1.joinRoomController);
99
router.route("/all").get(authenticateUser_1.authenticateUser, roomControllers_1.fetchAllRoomsController);
10+
router.route("/:roomId").get(authenticateUser_1.authenticateUser, roomControllers_1.fetchRoomByIdController);
1011
exports.default = router;

apps/ws-server/dist/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ wss.on("connection", (socket, req) => __awaiter(void 0, void 0, void 0, function
6262
break;
6363
case "disconnect_room":
6464
for (const [roomId, connections] of activeRooms.entries()) {
65+
const isMember = connections.some((conn) => conn.socket === socket);
66+
if (isMember) {
67+
connections.forEach((member) => {
68+
if (member.socket !== socket) {
69+
member.socket.send(JSON.stringify({
70+
type: "disconnect_room",
71+
userId: validMessage.data.userId,
72+
roomId: roomId,
73+
}));
74+
}
75+
});
76+
}
6577
const updatedConnections = connections.filter((conn) => conn.socket !== socket);
6678
if (updatedConnections.length === 0) {
6779
activeRooms.delete(roomId);
@@ -194,13 +206,47 @@ wss.on("connection", (socket, req) => __awaiter(void 0, void 0, void 0, function
194206
}
195207
break;
196208
}
209+
case "cursor": {
210+
const socketList = activeRooms.get(validMessage.data.roomId);
211+
if (!(socketList === null || socketList === void 0 ? void 0 : socketList.some((conn) => conn.userId === validMessage.data.userId && conn.socket === socket))) {
212+
socket.send(JSON.stringify({
213+
type: "error_message",
214+
content: "Not connected to the room",
215+
}));
216+
return;
217+
}
218+
// Broadcast cursor position purely to other users in the room
219+
socketList === null || socketList === void 0 ? void 0 : socketList.forEach((member) => {
220+
if (member.socket !== socket) {
221+
member.socket.send(JSON.stringify({
222+
type: "cursor",
223+
userId: validMessage.data.userId,
224+
roomId: validMessage.data.roomId,
225+
content: validMessage.data.content,
226+
}));
227+
}
228+
});
229+
break;
230+
}
197231
}
198232
}));
199233
socket.on("close", () => {
200234
const status = userVerificationStatus.get(socket);
201235
console.log(`[WS Server] Connection closed for User: ${(status === null || status === void 0 ? void 0 : status.userId) || 'unverified'}`);
202236
userVerificationStatus.delete(socket);
203237
for (const [roomId, connections] of activeRooms.entries()) {
238+
const isMember = connections.some((conn) => conn.socket === socket);
239+
if (isMember && (status === null || status === void 0 ? void 0 : status.userId)) {
240+
connections.forEach((member) => {
241+
if (member.socket !== socket) {
242+
member.socket.send(JSON.stringify({
243+
type: "disconnect_room",
244+
userId: status.userId,
245+
roomId: roomId,
246+
}));
247+
}
248+
});
249+
}
204250
const updatedConnections = connections.filter((conn) => conn.socket !== socket);
205251
if (updatedConnections.length === 0) {
206252
activeRooms.delete(roomId);

0 commit comments

Comments
 (0)