diff --git a/src/pages/chat/[apply_id]/Chatting.tsx b/src/pages/chat/[apply_id]/Chatting.tsx index c9efbcc..f932327 100644 --- a/src/pages/chat/[apply_id]/Chatting.tsx +++ b/src/pages/chat/[apply_id]/Chatting.tsx @@ -2,7 +2,6 @@ import { Avatar, Stack } from "@mui/material"; import { useEffect, useCallback, useRef, useState } from "react"; import { useParams } from "react-router-dom"; import SendIcon from '@mui/icons-material/Send'; -import { Button } from "../../../components/common"; type ChattingPageParams = { apply_id : string; } @@ -11,20 +10,46 @@ export const Chatting = () => { const [inputHeight, setInputHeight] = useState(60); // 초기값은 입력 부분의 높이 //const [msgInput, setMsgInput] = useState(''); const [msg, setMsg] = useState(''); - const message="안녕하세요! " - const [name, setName] = useState('수민'); + const [chat, setChat] = useState([]); + //const message="안녕하세요! " + const [name, setName] = useState(''); const [chkLog, setChkLog] = useState(false); const [socketData, setSocketData] = useState(); const ws = useRef(null); + const messageStyle = { maxWidth:' 70%', //`${message.length * 15}px`, // 예시: 글자 수에 따라 동적으로 설정 }; + useEffect(() => { + const fetchNickname = async () =>{ + try{ + const response = await fetch(`http://localhost:8080/nickname`,{ + method: 'GET', + credentials: 'include', + }); + + // Check if login is successful, then redirect to StarPage + if (response.ok) { + const nicknameData = await response.text(); + console.log(nicknameData); + setName(nicknameData); + console.log(name); + }else{ + console.error('Failed to fetch nickname : ',response.status, response.statusText); + } + } catch (error) { + console.error('Failed to fetch nickname : ', error); + } + }; + fetchNickname(); + },[]); + useEffect(() => { const handleResize = () => { setInputHeight(window.innerHeight * 0.1); // 예시: 뷰포트 높이의 10% @@ -54,8 +79,13 @@ export const Chatting = () => { // @ts-ignore ws.current.onmessage = (message) => { console.log('WebSocket message received:', message.data); - //const newMessage = JSON.parse(message.data); - //setChatt((prevChatt) => [...prevChatt, newMessage]); + const newMessage = JSON.parse(message.data); + // @ts-ignore + setChat((prevChat) => { + const newChat = [...prevChat, newMessage]; + console.log('New chat state:', newChat); // 로깅 추가 + return newChat; + }); }; // @ts-ignore ws.current.onclose = (event: any) => { @@ -95,7 +125,18 @@ export const Chatting = () => { // @ts-ignore ws.current.send(temp); } else { - console.warn('WebSocket is not open'); + console.warn('WebSocket is not open, waiting for connection to open...'); + // Wait for the connection to open + setTimeout(() => { + // Check again and send the message if the connection is open + // @ts-ignore + if (ws.current.readyState === WebSocket.OPEN) { + // @ts-ignore + ws.current.send(temp); + } else { + console.error('WebSocket is still not open. Message not sent.'); + } + }, 1000); // Adjust the timeout duration as needed } } else { alert('메세지를 입력하세요.'); @@ -106,71 +147,34 @@ export const Chatting = () => { setMsg(''); }, [chkLog, msg, name, webSocketLogin]); -const handleConfirm = () => { - console.log('눌림') - -} const buttonColor = msg == '' ? 'bg-zinc-100' : 'bg-primary-logo' - return ( -