-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from pepero-1/feature/#1
소켓 통신 useSocket 추가, 토스트 알람 UI 일부 수정
- Loading branch information
Showing
6 changed files
with
149 additions
and
74 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
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
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,42 @@ | ||
import { useEffect } from "react"; | ||
import { io } from "socket.io-client"; | ||
|
||
interface Message { | ||
id: string; | ||
text: string; | ||
userId: string; | ||
createdAt: Date; | ||
} | ||
|
||
const useSocket = ( | ||
chatId: string, | ||
callback?: (messageObject: Message) => void, | ||
) => { | ||
const token = JSON.parse(localStorage.getItem("token") as string); | ||
|
||
// 소켓 연결 | ||
const socket = io(`https://fastcampus-chat.net/chat?chatId=${chatId}`, { | ||
extraHeaders: { | ||
Authorization: `Bearer ${token.accessToken}`, | ||
serverId: import.meta.env.VITE_APP_SERVER_ID, | ||
}, | ||
}); | ||
|
||
// 변화 감지 | ||
useEffect(() => { | ||
if (callback) { | ||
socket.on("message-to-client", (messageObject) => { | ||
callback(messageObject); | ||
}); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [socket]); | ||
// 메시지 보내기 | ||
const sendMessage = (text: string) => { | ||
socket.emit("message-to-server", text); | ||
}; | ||
|
||
return sendMessage; | ||
}; | ||
|
||
export default useSocket; |
Oops, something went wrong.