Skip to content

Commit

Permalink
feat: 채팅 라이브러리 import 성공 (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyuhho committed Jan 27, 2024
1 parent 24321ee commit 5224f7a
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 123 deletions.
41 changes: 41 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"@mui/material": "^5.15.3",
"@stomp/stompjs": "^7.0.0",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.6",
"@types/react": "^18.2.46",
Expand All @@ -21,6 +22,7 @@
"react-textarea-autosize": "^8.5.3",
"recoil": "^0.7.7",
"recoil-persist": "^5.1.0",
"sockjs-client": "^1.6.1",
"styled-components": "^6.1.0",
"styled-normalize": "^8.1.0",
"typescript": "^4.9.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const CounselorReview = ({ counselorId }: CounselorReviewProps) => {
const fetchReviewData = async () => {
setIsLoading(true);
const params = {
cursorId: 0,
reviewId: 0,
};
const res: any = await getReviews(counselorId, { params });
if (res.status === 200) {
Expand Down
179 changes: 61 additions & 118 deletions src/pages/Buyer/BuyerChat.tsx
Original file line number Diff line number Diff line change
@@ -1,121 +1,64 @@
import { useState } from 'react';
// import { useState } from 'react';
import SockJs from 'sockjs-client';
import Stomp from 'stompjs';

import * as StompJs from '@stomp/stompjs';
export const BuyerChat = () => {
// const [stompClient, setStompClient] = useState(null);
// const [chatId] = useState(20);
// const [isCustomer] = useState(true);
// const [token, setToken] = useState('');
// const [messages, setMessages] = useState([]);
// const [messageInput, setMessageInput] = useState('');
// const connect = () => {
// const socket = new SockJs('/chat');
// const client = Stomp.over(socket);
// client.connect(
// {
// Authorization: 'Bearer ' + token,
// isCustomer: isCustomer,
// },
// (frame) => {
// console.log('Connected: ' + frame);
// // Subscribe to channels
// client.subscribe(
// '/queue/chattings/notifications/customers/19',
// (notification) => {
// console.log('Notification: ', notification.body);
// },
// );
// client.subscribe(
// '/queue/chattings/customers/' + chatId,
// (statusUpdate) => {
// console.log('Status Update: ', statusUpdate.body);
// },
// );
// client.subscribe(
// '/queue/chattings/status/customers/' + chatId,
// (statusAutoUpdate) => {
// console.log('Status Auto Update: ', statusAutoUpdate.body);
// },
// );
// client.subscribe(
// '/queue/chattings/exception/customers/' + chatId,
// (error) => {
// console.log('Error: ', error.body);
// },
// );
// client.subscribe(
// '/queue/chatMessages/customers/' + chatId,
// (message) => {
// console.log('Message: ', message.body);
// displayMessage(message.body);
// },
// );
// setStompClient(client);
// },
// );
// };
// const sendMessage = () => {
// if (stompClient !== null) {
// stompClient.send(
// `/app/api/v1/chatMessages/customers/${chatId}`,
// {},
// JSON.stringify({ content: messageInput }),
// );
// }
// };
// const sendChatStartRequest = () => {
// stompClient.send(
// `/app/api/v1/chat/customers/${chatId}`,
// {},
// JSON.stringify({ chatWebsocketStatus: 'COUNSELOR_CHAT_START_REQUEST' }),
// );
// };
// const sendChatStartResponse = () => {
// stompClient.send(
// `/app/api/v1/chat/customers/${chatId}`,
// {},
// JSON.stringify({ chatWebsocketStatus: 'CUSTOMER_CHAT_START_RESPONSE' }),
// );
// };
// const sendChatFinishRequest = () => {
// stompClient.send(
// `/app/api/v1/chat/customers/${chatId}`,
// {},
// JSON.stringify({ chatWebsocketStatus: 'CUSTOMER_CHAT_FINISH_REQUEST' }),
// );
// };
// const displayMessage = (message) => {
// setMessages([...messages, message]);
// };
return (
<></>
// <div>
// <h2>Customer Chat</h2>
// <div>
// <label htmlFor="tokenInput">Token:</label>
// <input
// type="text"
// id="tokenInput"
// value={token}
// onChange={(e) => setToken(e.target.value)}
// />
// <button onClick={connect}>Connect</button>
// </div>
// <div id="messages">
// {messages.map((message, index) => (
// <div key={index}>{message}</div>
// ))}
// </div>
// <input
// type="text"
// value={messageInput}
// onChange={(e) => setMessageInput(e.target.value)}
// />
// <button onClick={sendMessage}>Send Message</button>
// <button onClick={sendChatStartRequest}>Start Chat Request</button>
// <button onClick={sendChatStartResponse}>Start Chat Response</button>
// <button onClick={sendChatFinishRequest}>End Chat</button>
// </div>
);
const socket = new SockJs('/chat');
const stompClient = StompJs.Stomp.over(socket);
const chatId = 20;
const headers = {
Authorization:
'Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJjaGF0QGdtYWlsLmNvbSIsImF1dGhvcml0aWVzIjoiUk9MRV9DVVNUT01FUiIsImV4cCI6MTcwNjMyNDE0Nn0.NL23laiKzJFOlzyvgSMwXcm5Q-NsFqmu6PYFhvbl_QQ',
isCustomer: 'true',
};

stompClient.configure({
brokerURL: '/chat',
connectHeaders: headers,
reconnectDelay: 5000,
heartbeatIncoming: 4000,
heartbeatOutgoing: 4000,
onConnect: (frame) => {
console.log('Connected: ' + frame);

// 구독
stompClient.subscribe(
'/queue/chattings/notifications/customers/19',
(notification) => {
console.log('Notification: ', notification.body);
},
);

stompClient.subscribe(
'/queue/chattings/customers/' + chatId,
(statusUpdate) => {
console.log('Status Update: ', statusUpdate.body);
},
);

stompClient.subscribe(
'/queue/chattings/status/customers/' + chatId,
(statusAutoUpdate) => {
console.log('Status Auto Update: ', statusAutoUpdate.body);
},
);

stompClient.subscribe(
'/queue/chattings/exception/customers/' + chatId,
(error) => {
console.log('Error: ', error.body);
},
);

stompClient.subscribe(
'/queue/chatMessages/customers/' + chatId,
(message) => {
console.log('Message: ', message.body);
// displayMessage(message.body);
},
);
},
});

stompClient.activate();
return <></>;
};
3 changes: 1 addition & 2 deletions src/pages/Buyer/BuyerReviewManage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ export const BuyerReviewManage = () => {
const setScrollLock = useSetRecoilState(scrollLockState);
useLayoutEffect(() => {
const fetchReviewData = async () => {
const params = { isCompleted: !isReviewWrite, cursorId: 0 };
const params = { isCompleted: !isReviewWrite, reviewId: 0 };
const res: any = await getReviewsCustomer({ params });
if (res.status === 200) {
console.log(res.data);
setReviewData(res.data);
} else if (res.response.status === 404) {
alert('존재하지 않는 회원입니다.');
Expand Down
1 change: 1 addition & 0 deletions src/pages/Buyer/BuyerSavedCounselor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const BuyerSavedCounselor = () => {
const res: any = await postWishLists(body);
if (res.status === 200) {
setWishlistData(res.data);
console.log(res.data[res.data.length]);
} else if (res.response.status !== 401) {
navigate('/buyer/mypage');
}
Expand Down
2 changes: 0 additions & 2 deletions src/socket.d.ts

This file was deleted.

0 comments on commit 5224f7a

Please sign in to comment.