From 25f64a0de3c2e1779430dc31b1fffa266d3642cc Mon Sep 17 00:00:00 2001 From: munee <121735319+alaneelee@users.noreply.github.com> Date: Wed, 27 Dec 2023 01:56:38 +0900 Subject: [PATCH 1/7] =?UTF-8?q?=EC=A4=91=EB=B3=B5=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EA=B5=AC=EB=8F=85=EB=90=98=EB=8A=94=20=EB=AC=B8=EC=A0=9C=3F=20?= =?UTF-8?q?=EB=B0=9C=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Chat.js | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/components/Chat.js diff --git a/src/components/Chat.js b/src/components/Chat.js new file mode 100644 index 0000000..0ec540f --- /dev/null +++ b/src/components/Chat.js @@ -0,0 +1,79 @@ +import React, { useEffect, useState } from 'react'; + +const ChatComponent = () => { + const [messages, setMessages] = useState([]); + const [inputMessage, setInputMessage] = useState(''); + const [socket, setSocket] = useState(null); + + useEffect(() => { + const roomId = 'abcde'; + const newSocket = new WebSocket(`ws://localhost:8082/chat/${roomId}`); + setSocket(newSocket); + + newSocket.onopen = () => { + console.log('웹소켓 연결됨'); + }; + + newSocket.onmessage = (event) => { + console.log(event.data); + const receiveData = event.data.split(':'); + const from = receiveData[0]; + const message = receiveData.slice(1).join(':').trim(); + setMessages((prevMessages) => [...prevMessages, { from, message }]); + }; + + newSocket.onclose = () => { + console.log('웹소켓 연결 종료'); + // 연결 종료 시 재연결 시도 + setTimeout(() => { + const reconnectSocket = new WebSocket(`ws://localhost:8082/chat/${roomId}`); + reconnectSocket.onopen = () => { + console.log('웹소켓 재연결됨'); + setSocket(reconnectSocket); + }; + + reconnectSocket.onmessage = (event) => { + console.log(event.data); + const receiveData = event.data.split(':'); + const from = receiveData[0]; + const message = receiveData.slice(1).join(':').trim(); + setMessages((prevMessages) => [...prevMessages, { from, message }]); + }; + }, 6000); // 6초 후 재연결 시도 + }; + + return () => { + newSocket.close(); + }; + }, []); + + const sendMessage = () => { + if (inputMessage.trim() === '') return; + + socket.send(inputMessage); + setInputMessage(''); + }; + + return ( +
{text}
From 64d15112c98932e75d687e486f0094541870949d Mon Sep 17 00:00:00 2001 From: munee <121735319+alaneelee@users.noreply.github.com> Date: Thu, 28 Dec 2023 13:54:31 +0900 Subject: [PATCH 5/7] chat UI --- src/components/Chat.js | 134 +++++++++++++++++++++++++++---------- src/pages/ChannelDetail.js | 36 +++++----- 2 files changed, 119 insertions(+), 51 deletions(-) diff --git a/src/components/Chat.js b/src/components/Chat.js index 4c7be11..e935e72 100644 --- a/src/components/Chat.js +++ b/src/components/Chat.js @@ -1,46 +1,103 @@ -import React, { useEffect, useState } from "react"; -import { useRef } from "react"; +import React, { useEffect, useState, useRef } from 'react'; +import styled from 'styled-components'; + +const ChatContainer = styled.div` + border: 1px solid #ccc; + width: 99.7%; + height: 100%; + margin: 0 auto; + display: flex; + flex-direction: column; +`; + +const MessageContainer = styled.div` + margin-bottom: 10px; +`; + +const MessageBox = styled.div` + overflow-y: auto; +`; + +const Sender = styled.span` + color: #c0c0c0; + font-size: 1rem; + padding-left: 5px; + font-family: 'Gamja Flower', sans-serif; +`; + +const SpanText = styled.span` + font-size: 1.1rem; + color: #666; + font-family: 'Gamja Flower', sans-serif; +`; + +const InputContainer = styled.div` + margin-top: auto; + border-top: 1px solid #333333; + height: 8vh; + display: flex; + align-items: center; + justify-content: center; +`; + +const TextInput = styled.input` + border-radius: 10px; + width: 70%; + height: 10%; + padding: 10px; + margin-right: 5px; +`; + +const SendButton = styled.button` + font-family: 'Gamja Flower', sans-serif; + border-radius: 10px; + width: 20%; + height: 50%; + padding: 5px 10px; + background-color: #f8de7e; + color: #666; + border: none; + cursor: pointer; +`; const ChatComponent = ({ chatToken, chattingRoomId }) => { const [messages, setMessages] = useState([]); - const [inputMessage, setInputMessage] = useState(""); + const [inputMessage, setInputMessage] = useState(''); const [socket, setSocket] = useState(null); const tokenString = chatToken; + const messagesEndRef = useRef(null); useEffect(() => { // window.addEventListener("beforeunload", () => { // socket.close(); // }); - console.log("chatToken", chatToken); - console.log("chattingRommId", chattingRoomId); + const chattingRoomIdString = chattingRoomId; - const accessToken = localStorage.getItem("accessToken"); - console.log( - `ws://localhost:8082/chat/${chattingRoomIdString}/${tokenString}` - ); + const accessToken = localStorage.getItem('accessToken'); + const newSocket = new WebSocket( `ws://localhost:8082/chat/${chattingRoomIdString}/${tokenString}` ); setSocket(newSocket); newSocket.onopen = () => { - console.log("웹소켓 연결됨"); + console.log('웹소켓 연결됨'); }; newSocket.onmessage = (event) => { console.log(event.data); - const receiveData = event.data.split(":"); + const receiveData = event.data.split(':'); const from = receiveData[0]; - const message = receiveData.slice(1).join(":").trim(); + const message = receiveData.slice(1).join(':').trim(); setMessages((prevMessages) => [...prevMessages, { from, message }]); }; newSocket.onclose = () => { - console.log("웹소켓 연결 종료"); + console.log('웹소켓 연결 종료'); // 연결 종료 시 재연결 시도 setTimeout(async () => { - console.log("accessToken", accessToken); - const response = await fetch("http://localhost:8081/api/auth/chat", { - method: "POST", + console.log('accessToken', accessToken); + const response = await fetch('http://localhost:8081/api/auth/chat', { + method: 'POST', headers: { Authorization: accessToken, }, @@ -52,15 +109,15 @@ const ChatComponent = ({ chatToken, chattingRoomId }) => { `ws://localhost:8082/chat/${chattingRoomIdString}/${tokenString}` ); reconnectSocket.onopen = () => { - console.log("웹소켓 재연결됨"); + console.log('웹소켓 재연결됨'); setSocket(reconnectSocket); }; reconnectSocket.onmessage = (event) => { console.log(event.data); - const receiveData = event.data.split(":"); + const receiveData = event.data.split(':'); const from = receiveData[0]; - const message = receiveData.slice(1).join(":").trim(); + const message = receiveData.slice(1).join(':').trim(); setMessages((prevMessages) => [...prevMessages, { from, message }]); }; }, 6000); // 6초 후 재연결 시도 @@ -72,31 +129,40 @@ const ChatComponent = ({ chatToken, chattingRoomId }) => { }, []); const sendMessage = () => { - if (inputMessage.trim() === "") return; + if (inputMessage.trim() === '') return; socket.send(inputMessage); - setInputMessage(""); + setInputMessage(''); + }; + + const handleKeyDown = (event) => { + if (event.key === 'Enter') { + event.preventDefault(); + sendMessage(); + } }; return ( -