Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hotfix] connect when login #166

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/contexts/StompContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, {
ReactNode,
useRef,
MutableRefObject,
useState,
} from 'react';
import SockJs from 'sockjs-client';
import { CompatClient, Stomp } from '@stomp/stompjs';
Expand All @@ -19,7 +20,8 @@ https://stackoverflow.com/questions/46662524/java-spring-boot-websocket-communic

interface StompProviderType {
stompClient: MutableRefObject<CompatClient | null>;
// connect: (roomId: number) => void;
connectChat: () => void;
isConnected: boolean;
// disconnect: () => void;
// sendMessage: (message: Message) => void;
}
Expand All @@ -35,9 +37,9 @@ export const StompProvider: React.FC<{ children: ReactNode }> = ({
children,
}) => {
const stompClient = useRef<CompatClient | null>(null);
// const [isConnected, setIsConnected] = useState(false);
const { pathname } = useLocation();
const [isCustomer, setIsCustomer] = useRecoilState(isCustomerState);
const [isConnected, setIsConnected] = useState<boolean>(false);

// 옯바른 소켓 연결을 위해 경로에 따라 마인더, Seller 구분
if (pathname.includes('/minder')) {
Expand Down Expand Up @@ -73,8 +75,10 @@ export const StompProvider: React.FC<{ children: ReactNode }> = ({
},
(frame: any) => {
console.log('Connected: ' + frame);
setIsConnected(true);
},
(error: any) => {
setIsConnected(false);
if (error.headers.message === 'UNAUTHORIZED') {
reissueToken();
} else {
Expand All @@ -98,7 +102,7 @@ export const StompProvider: React.FC<{ children: ReactNode }> = ({
}, [isCustomer]); // 한 번만 실행
//Context 객체의 Provider 컴포넌트
return (
<StompContext.Provider value={{ stompClient }}>
<StompContext.Provider value={{ stompClient, connectChat, isConnected }}>
{children}
</StompContext.Provider>
);
Expand Down
5 changes: 5 additions & 0 deletions src/pages/Buyer/BuyerLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { LoginModal } from 'components/Buyer/BuyerLogin/LoginModal';
import { useState } from 'react';
import PwInput from 'components/Buyer/Common/PwInput';
import { Space } from 'components/Common/Space';
import { useStompContext } from 'contexts/StompContext';
export const BuyerLogin = () => {
const emailInput = useInput('');
const pwInput = useInput('');
Expand All @@ -19,6 +20,9 @@ export const BuyerLogin = () => {
const [isActiveModal, setIsActiveModal] = useState<boolean>(false);
// 모달 에러 메세지
const [modalErrorMessage, setModalErrorMessage] = useState<string>('');

const { connectChat } = useStompContext();

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();

Expand All @@ -36,6 +40,7 @@ export const BuyerLogin = () => {
// setCookie('refreshToken', newRefreshToken, { path: '/' });
localStorage.setItem('accessToken', newAccessToken);
localStorage.setItem('refreshToken', newRefreshToken);
connectChat();
navigate('/share');
} else if (res.response.status === 400) {
setIsActiveModal(true);
Expand Down
Loading