-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
사용자가 메시지를 보낼 때 다른 방에도 메시지가 보내지는 이슈를 해결했습니다. 원인은 방을 생성하거나 채팅을 보낼때 전역적으로 socketsJoin을 사용하여 채팅방에 활성된 유저가 초대되었기 때문인데… 이를 해결하기 위해 사용자가 채팅방 페이지에 접속할 때 GET /room/[room_id]를 실행시킴으로써 해당 socket만 채팅방에 접속하는 로직을 추가함으로써 해당 이슈를 해결했습니다. 기존에 채팅 로그를 가져오던 API는 GET /room/[room_id]/chats 로 이동시켜 API와의 충돌을 막았습니다.
- Loading branch information
PINOT
committed
Feb 28, 2022
1 parent
995a347
commit 85c1e5f
Showing
5 changed files
with
40 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { NextApiRequest } from "next"; | ||
import { NextApiResponseServerIO } from "types/next"; | ||
import { prisma } from "lib/prisma"; | ||
|
||
export default async (req: NextApiRequest, res: NextApiResponseServerIO) => { | ||
const room_id = String(req.query.room_id); | ||
|
||
if (req.method === "GET") { | ||
const response = await prisma.chat.findMany({ | ||
where: { | ||
room_id, | ||
}, | ||
}); | ||
|
||
res.json(response); | ||
} else { | ||
// block if method is not supported | ||
return res.status(405).end(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
import { NextApiRequest } from "next"; | ||
import { NextApiResponseServerIO } from "types/next"; | ||
import { prisma } from "lib/prisma"; | ||
|
||
export default async (req: NextApiRequest, res: NextApiResponseServerIO) => { | ||
const room_id = String(req.query.room_id); | ||
const { room_id } = req.query; | ||
|
||
if (req.method === "GET") { | ||
const response = await prisma.chat.findMany({ | ||
where: { | ||
room_id, | ||
}, | ||
res.socket.server.io.once("connection", (socket) => { | ||
console.log(`client ${socket.id} has connected`); | ||
|
||
// do not make duplicate join chat-room | ||
if (!socket.rooms.has(String(room_id))) { | ||
console.log(`client ${socket.id} has joined ${room_id}`); | ||
socket.join(room_id); | ||
} | ||
|
||
// handle disconnect | ||
socket.on("disconnect", (reason) => { | ||
console.log(`client ${socket.id} has disconnected ${reason}`); | ||
socket.leave(String(room_id)); | ||
}); | ||
}); | ||
|
||
res.json(response); | ||
res.end(); | ||
} else { | ||
// block if method is not supported | ||
return res.status(405).end(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters