-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.tsx
121 lines (108 loc) · 3.26 KB
/
server.tsx
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
import { createServer } from "http";
import { parse } from "url";
import next from "next";
import { Server, Socket } from "socket.io";
const port = process.env.PORT || 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = createServer((req, res) => {
const parsedUrl = parse(req.url!, true);
handle(req, res, parsedUrl);
});
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
io.on("connection", (socket: Socket) => {
console.log("A user connected");
socket.on("joinRoom", (appID: string) => {
socket.join(appID);
console.log(`User with appID ${appID} joined their room`);
});
socket.on(
"sendMessage",
(data: {
senderID: string;
receiverID: string;
senderAppID: string;
receiverAppID: string;
message: string;
conversationId: string;
messageId: number;
}) => {
console.log(
`Message from ${data.senderID} to ${data.receiverID}: "${data.message}"`,
);
io.to(data.receiverAppID).emit("receiveMessage", {
message: data.message,
senderID: data.senderID,
receiverID: data.receiverID,
timestamp: new Date(),
conversationId: data.conversationId,
messageId: data.messageId,
});
console.log(
`Message emitted to rooms with appID: ${data.receiverAppID}`,
);
},
);
socket.on(
"deleteMessage",
(data: { messageId: number; receiverAppID: string }) => {
console.log(`Deleting message with ID: ${data.messageId}`);
io.to(data.receiverAppID).emit("messageDeleted", {
messageId: data.messageId,
});
},
);
socket.on(
"updateUnreadCount",
(data: { friendId: string; unreadCount: number }) => {
console.log("id: ", data.friendId);
console.log("unreadcount: ", data.unreadCount);
io.to(data.friendId).emit("updateFriendUnread", {
userId: data.friendId,
unreadCount: data.unreadCount
});
},
);
socket.on(
"unsendMessage",
(data: {
messageId: number;
senderAppID: string;
receiverAppID: string;
conversationId: string;
}) => {
console.log(
`Received unsendMessage at ${new Date().toISOString()} for ID: ${data.messageId}`,
);
io.to(data.senderAppID).emit("messageUnsent", {
messageId: data.messageId,
conversationId: data.conversationId,
});
if (data.senderAppID !== data.receiverAppID) {
console.log("halo");
io.to(data.receiverAppID).emit("messageUnsent", {
messageId: data.messageId,
conversationId: data.conversationId,
});
}
console.log(
`Message unsending to rooms with appID: ${data.receiverAppID} and ${data.senderAppID}`,
);
},
);
socket.on("disconnect", () => {
console.log("A user disconnected");
});
});
server.listen(3000, (err?: any) => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});