-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
137 lines (118 loc) · 3.51 KB
/
index.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import express from "express";
import http from "http";
import { WebSocketServer } from "ws";
import cors from "cors";
import { RedisSubscriptionManager } from "./subscriptions/RedisSubscriptionManager";
import { PrismaClient } from "@prisma/client";
import userRouter from "./routes/userRouter";
import roomRouter from "./routes/roomRouter";
import chatsRouter from "./routes/chatRouter";
const app = express();
const port = 3001;
const prisma = new PrismaClient();
app.use(express.json());
app.use(cors());
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
app.use('/user',userRouter);
app.use('/room',roomRouter);
app.use('/chats', chatsRouter);
interface MessagePayload {
username: string;
roomId: number;
content: string;
}
interface MessageData {
type: 'join' | 'message';
payload: MessagePayload;
}
wss.on("connection", async (ws) => {
let userId: number = 0;
let roomId: number = 0;
let username: string = "";
ws.on("message", async (message: string) => {
const data: MessageData = JSON.parse(message);
if (data.type === "join") {
username = data.payload.username;
roomId = data.payload.roomId;
userId = (await getUserId(username)) as number;
if (userId !== null) {
const existingUserRoom = await prisma.userRooms.findUnique({
where: {
userId_roomId: {
userId: userId,
roomId: roomId,
},
},
});
if (!existingUserRoom) {
await prisma.userRooms.create({
data: {
userId: userId,
roomId: roomId,
},
});
console.log(`User ${userId} joined room ${roomId}`);
} else {
console.log(`User ${userId} is already part of room ${roomId}`);
}
RedisSubscriptionManager.getInstance().subscribe(
userId.toString(),
roomId.toString(),
ws
);
} else {
console.log(`User ${username} not found.`);
}
}
if (data.type === "message") {
roomId = data.payload.roomId;
const content = data.payload.content;
const username = data.payload.username;
const userId = (await getUserId(username)) as number;
console.log(`Attempting to create message for userId: ${userId}, roomId: ${roomId}`);
// Check if roomId exists in the Room table
const roomExists = await prisma.room.findUnique({
where: { id: roomId },
});
if (roomExists) {
const newMessage = await prisma.message.create({
data: {
userId: userId,
roomId: roomId,
content: data.payload.content,
createdAt: new Date(),
},
});
RedisSubscriptionManager.getInstance().addChatMessage(
roomId.toString(),
JSON.stringify({
type: "message",
payload: {
userId: userId,
content: newMessage.content,
createdAt: newMessage.createdAt,
},
})
);
} else {
console.log(`Room ${roomId} does not exist.`);
}
}
});
});
async function getUserId(username: string): Promise<number | null> {
const user = await prisma.user.findUnique({
where: { username },
select: {
id: true
}
});
return user ? user.id : null;
}
app.listen(3000, () => {
console.log(`Express server is listening on port ${port}`);
});
server.listen(3001, () => {
console.log(`Server is listening on port ${port}`);
});