diff --git a/src/components/Game/GameChat/index.tsx b/src/components/Game/GameChat/index.tsx index 8cd19f3e..391e9a12 100644 --- a/src/components/Game/GameChat/index.tsx +++ b/src/components/Game/GameChat/index.tsx @@ -1,5 +1,95 @@ -const GameChat = () => { - return
game chat
; +import { + Button, + Card, + CardBody, + Input, + InputGroup, + InputRightElement, +} from "@chakra-ui/react"; +import { useEffect, useState } from "react"; +import { io } from "socket.io-client"; +import useInput from "../../../hooks/useInput"; +import ChatBubble from "../../common/ChatBubble"; + +interface Message { + id: string; + text: string; +} +const GameChat = ({ gameId }) => { + console.log(gameId); + const token = JSON.parse(localStorage.getItem("token") as string); + + const socket = io(`https://fastcampus-chat.net/chat?chatId=${gameId}`, { + extraHeaders: { + Authorization: `Bearer ${token.accessToken}`, + serverId: import.meta.env.VITE_APP_SERVER_ID, + }, + }); + + // 메세지 데이터 + const [message, setMessage] = useState({ + id: "", + text: "", + }); + const [messages, setMessages]: any = useState([]); + const messageValue = useInput(""); + + useEffect(() => { + socket.on("message-to-client", (messageObject) => { + // 메시지 데이터, 작성 유저 상태 저장 + const copy = { ...message }; + copy.id = messageObject.userId; + copy.text = messageObject.text; + setMessage(copy); + }); + + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [socket]); + + // 메시지 값 변화시(소켓 통신 시) 콘솔에 메시지 데이터 출력 + useEffect(() => { + if (message.id !== "") { + console.log(message); + setMessages([...messages, message]); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [message.text]); + + const submitMessage = () => { + socket.emit("message-to-server", messageValue.value); + messageValue.clear(); + }; + + const handleKeyPress = (event: React.KeyboardEvent) => { + if (event.key === "Enter") { + submitMessage(); + } + }; + + return ( + + + {messages.map((message, index) => ( + + ))} + + + + + + + + + ); }; export default GameChat; diff --git a/src/components/common/ChatBubble.tsx b/src/components/common/ChatBubble.tsx new file mode 100644 index 00000000..fb69f0d1 --- /dev/null +++ b/src/components/common/ChatBubble.tsx @@ -0,0 +1,32 @@ +import { BoxProps, Container, Text } from "@chakra-ui/react"; +import React from "react"; +import styled from "styled-components"; + +const Bubble = styled.div` + margin: 8px 0px 12px; + padding: 8px; + background-color: #e2e8f0; + width: fit-content; + max-width: 300px; + border-radius: 0 12px 12px 12px; +`; + +interface ChatBubbleProps extends BoxProps { + userId?: string; + text: string; +} + +const ChatBubble: React.FC = ({ userId, text, ...rest }) => { + return ( + + + {userId && `${userId}`} + + + {text} + + + ); +}; + +export default ChatBubble; diff --git a/src/hooks/useInput.ts b/src/hooks/useInput.ts index ee21ffeb..bd25a87b 100644 --- a/src/hooks/useInput.ts +++ b/src/hooks/useInput.ts @@ -6,8 +6,11 @@ const useInput = (initValue: string) => { const handleChange = (e: ChangeEvent) => { setInputValue(e.target.value); }; + const clear = () => { + setInputValue(""); + }; - return { value: inputValue, onChange: handleChange }; + return { value: inputValue, onChange: handleChange, clear }; }; export default useInput; diff --git a/src/pages/Example/index.tsx b/src/pages/Example/index.tsx index de732e54..f291ffe2 100644 --- a/src/pages/Example/index.tsx +++ b/src/pages/Example/index.tsx @@ -70,7 +70,7 @@ const Example = () => { url: "https://fastcampus-chat.net/chat/leave", method: "PATCH", data: { - chatId: "0d9bb525-9766-40e2-bb11-9f10f9fe8839", + chatId: "e6d8fd5b-00e3-4598-b826-11366c8c4676", }, start: false, }); diff --git a/src/pages/Game/index.tsx b/src/pages/Game/index.tsx index 898d1772..7621119b 100644 --- a/src/pages/Game/index.tsx +++ b/src/pages/Game/index.tsx @@ -1,3 +1,5 @@ +import GameChat from "../../components/Game/GameChat"; +import { Container } from "@chakra-ui/react"; import useFireFetch from "../../hooks/useFireFetch"; const Game = () => { @@ -10,7 +12,11 @@ const Game = () => { const gameData = fireFetch.useGetSome("game", "id", gameId as string); console.log(gameData.data); - return
game page
; + return ( + + + + ); }; export default Game;