Service ํจํค์ง
- ํ๋กํ ์ฝ body ๋ฌธ์์ด์ ์ฒ๋ฆฌํ๋ค.
- ์์ฒญ์ ๋ง๊ฒ
CustomResponse ๋ฅผ ๋ฆฌํดํ๋ค.
UserService, RoomService, ChatService ํด๋์ค๊ฐ ์๋ค.
1. UserService ํด๋์ค ์ฃผ์ ๊ธฐ๋ฅ
- ๋ก๊ทธ์ธ ์, userId ์ client์ OutputStream์ ๋งคํํ์ฌ ๋ฑ๋กํ๋ค.
HashMap<Long, PrintWriter> onlineUser;
Socket client = new ServerSocket(8000).accept();
out = new PrintWriter(client.getOutputStream(), true, StandardCharsets.UTF_8);
userId = login(User body).getUserId();
onlineUser.put(userId, out);
onlineUser ๋ฅผ ํตํด, ์จ๋ผ์ธ ์ ์ ๊ด๋ฆฌ.
- ์ค์๊ฐ ํต์ / ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
2. RoomService ํด๋์ค ์ฃผ์ ๊ธฐ๋ฅ
- ์ฑํ
๋ฐฉ์ ์ด๋ / ํด์ฅ ์, ์ฑํ
๋ฐฉ์ ์ํด ์๋ ์ ์ ์ค
onlineUser ์๊ฒ๋ง ์ค์๊ฐ์ผ๋ก ์ฑํ
์ ์ ์กํ๋ค.
3. ChatService ํด๋์ค ์ฃผ์ ๊ธฐ๋ฅ
- ์ฑํ
์ ์ก ์, ์ฑํ
๋ฐฉ์ ์ํด ์๋ ์ ์ ์ค
onlineUser ์๊ฒ๋ง ์ค์๊ฐ์ผ๋ก ์ฑํ
์ ์ ์กํ๋ค.
DAO ํจํค์ง
- ์ฟผ๋ฆฌ ์์ฑ ๋ฐ DB ์ฐ๋ ํจํค์ง์ด๋ค.
1. RoomDAO ํด๋์ค ์ฃผ์ ์ฟผ๋ฆฌ
- ์ฑํ
๋ฐฉ ๋ชฉ๋ก List๋ฅผ ์กฐํํ ๋
select room.roomId, room.roomName
from ChatRooms as room
join UserRooms as ur
on room.roomId = ur.roomId
where ur.userId = ?
2. ChatDAO ํด๋์ค ์ฃผ์ ์ฟผ๋ฆฌ
- ์ฑํ
๋ฐฉ ๋ฉ์ธ์ง ์กฐํ: List
select cr.roomName, users.nickname, chat.message
from Chats as chat
join Users as users on users.userId = chat.userId
join ChatRooms as cr on cr.roomId = chat.roomId
where chat.roomId = ?
- ๋ฉ์ธ์ง ์ ์ก
- ์ฑํ
๋ฐฉ ์ ์ ๋ค์๊ฒ ๋ฉ์ธ์ง ์ ์ก์ ์ํด
- โ์ฑํ
๋ฐฉ ์ ์ ID: Longโ, โ๋ฐฉ ์ด๋ฆโ, โ์ฑํ
๋ฐ์กํ ์ ์ ๋๋ค์โ ์ ์กฐํ ํ๊ณ ์๋ค.
๐ย Github (Code Link)
ChatProgram