Skip to content

Commit

Permalink
Refactor code to add lazy loading for images in TalkListContent
Browse files Browse the repository at this point in the history
  • Loading branch information
tako0614 committed Jul 15, 2024
1 parent eb13732 commit 228b007
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
62 changes: 36 additions & 26 deletions routes/api/v2/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const maxMessage = Number(env["MAX_MESSAGE_LENGTH"]);
const subClient = redis.createClient({
url: redisURL,
});
const sessions = new Map<string, WebSocketSessionObject>();
/**メインコンテンツ開始 */
await subClient.connect();
async function subscribeMessage(channel: string | string[]) {
Expand All @@ -30,32 +31,41 @@ async function subscribeMessage(channel: string | string[]) {
if (!session) {
return;
}
const message = data.message;
if (typeof message !== "string") {
return;
}
if (message.length > maxMessage) {
return;
}
const roomid = session.roomid;
const room = await rooms.findOne({ uuid: roomid });
if (!room) {
return;
if(session.roomType === "friend") {
const message = data.message;
if (typeof message !== "string") {
return;
}
if (message.length > maxMessage) {
return;
}
const roomid = session.roomid;
const room = await rooms.findOne({ uuid: roomid });
if (!room) {
return;
}
await messages.create(
{
roomid: roomid,
userid: session.userid,
messageid: generate(),
message: message,
messageType: "text",
read: [
{
userid: session.userid,
},
],
},
);
//sessionsのroomidが一致するものに送信
sessions.forEach((session) => {
if (session.roomid === roomid) {
session.ws.send(JSON.stringify({ type: "text", message, userid: session.userName }));
}
});
break;
}
await messages.create(
{
roomid: roomid,
userid: session.userid,
messageid: generate(),
message: message,
messageType: "text",
read: [
{
userid: session.userid,
},
],
},
);
break;
}
default:
Expand All @@ -65,7 +75,6 @@ async function subscribeMessage(channel: string | string[]) {
}
/**メインコンテンツ終了*/
await subscribeMessage(redisch);
const sessions = new Map<string, WebSocketSessionObject>();
export const handler = {
GET(req: Request, ctx: any) {
if (!ctx.state.data.loggedIn) {
Expand Down Expand Up @@ -93,6 +102,7 @@ export const handler = {
return;
}
sessions.set(sessionid, {
userName: user.userName,
userid: isTrueSessionid.userid,
ws: socket,
roomid: "",
Expand Down
1 change: 1 addition & 0 deletions util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface WebSocketSessionObject {
ws: WebSocket;
roomid: string;
roomType: string;
userName: string;
lastActivityTime: Date;
}
export interface WebSocketJoiningRoom {
Expand Down

0 comments on commit 228b007

Please sign in to comment.