프론트에서 response를 제대로 받아오지 못하는 오류 발견
SuccessResponse <>() 안에 담아서 response 전달하는 방법으로 해결
@MessageMapping("/{categoryId}/send")
@SendTo("/{categoryId}/paging")
public SuccessResponse<Chatlog> handleWebSocketChat(@DestinationVariable int categoryId, @Payload Chatlog chatlog) {
chatService.saveChatlog(chatlog); // DB에 저장
return new SuccessResponse<>(chatlog); // WebSocket을 통해 다른 클라이언트에 전송
} //ChatRoomPage에 있는 ChatInputFrom component에서 요청을 하면 받는 곳
ChatListPage
return (
<div className="h-[calc(100%_-_64px)] text-white">
<div>
<ul>
{categories.map((category) => (
<li key={category.categoryId} className="p-4 bg-white shadow-md mb-2 rounded-lg">
<h2>{category.categoryName} 채팅방</h2>
<ChatPreview categoryId={category.categoryId}/>
</li>
))}
</ul>
</div>
</div>
);
ChatRoomPage
ChatPreview
useEffect(() => {
const fetchChatPreviews = async () => {
try {
const socket = new WebSocket('ws://localhost:5173/');
const response = await ChatApi.preview(categoryId);
if (response.data.length === 0) {
const stomp = Stomp.over(socket);
setStompClient(stomp); // Stomp 클라이언트 설정
stompClient.activate();
} else {
setChatPreviews(response.data);
}
} catch (error) {
console.log("채팅 미리보기 화면에 접근 불가", error);
}
};
fetchChatPreviews();
}, [categoryId]);
const onConnected = () => {
console.log("WebSocket 연결됨");
};
const onError = (error) => {
console.log("WebSocket 연결 오류", error);
};
const navigateToChatRoom = () => {
if (stompClient) {
navigate(`/chat/${categoryId}/paging`);
} else {
console.log("웹소켓 연결을 아직 기다리는 중입니다.");
}
};
Chatlog
useEffect(() => {
const fetchMoreChatlog = async () => {
if (loading) return;
setLoading(true);
try {
const response = await ChatApi.chatRoom(categoryId, page);
setChatlog((prevChatlog) => [...prevChatlog, ...response.data]);
setPage(prevPage => prevPage + 1);
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
};
const handleScroll = () => {
// 스크롤 이벤트 로직 (예: 페이지 맨 위에 도달했을 때 새 로그 불러오기)
if (window.scrollY === 0) {
fetchMoreChatlog();
}
};
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, [page, loading, categoryId]);
ChatInput
global is not defined
⇒ App.jsx 에서 아래 코드 추가 후 해결됨
if (typeof window !== 'undefined' && typeof window.global === 'undefined') {
window.global = window;
}
websocket is closed before the connection is established
GET 404, POST 404 에러 발생
ChatListPage의 ChatPreview 에 대해 버튼 클릭 시 (주소는 이동 했는데) ChatRoomPage로 화면이 이동하지 않음