1- import { useEffect } from 'react' ;
1+ import { v4 as uuidv4 } from 'uuid' ;
2+ import { useEffect , useCallback } from 'react' ;
23import { useSocket } from '../context/useSocket' ;
34import { ChatMessage } from '@service/feature/chat/schema/messageSchema.ts' ;
45
5- export const useChat = ( onMessage : ( msg : ChatMessage ) => void ) => {
6+ export const useChat = ( chatId : string | undefined , onMessage : ( msg : ChatMessage ) => void ) => {
67 const { client, isConnected } = useSocket ( ) ;
7- const chatId = '25ffc7bf-874f-444e-b331-26ed864a76ba' ;
88
99 useEffect ( ( ) => {
10- if ( ! client || ! isConnected ) return ;
10+ let subscription : any ;
11+
12+ const setupSubscription = ( ) => {
13+ if ( ! client || ! isConnected || ! chatId ) {
14+ console . log ( '채팅 구독 조건이 충족되지 않음:' , { client : ! ! client , isConnected, chatId } ) ;
15+ return ;
16+ }
17+
18+ try {
19+ const subscribeUrl = `/sub/message/${ chatId } ` ;
20+ console . log ( '채팅 구독 시도:' , subscribeUrl ) ;
21+
22+ subscription = client . subscribe ( subscribeUrl , ( message ) => {
23+ const parsed : ChatMessage = JSON . parse ( message . body ) ;
24+ onMessage ( parsed ) ;
25+ } ) ;
26+
27+ console . log ( '채팅 구독 성공' ) ;
28+ } catch ( error ) {
29+ console . error ( 'STOMP 구독 중 오류 발생:' , error ) ;
30+ }
31+ } ;
1132
12- const subscribeUrl = `/sub/message/${ chatId } ` ;
13- const subscription = client . subscribe ( subscribeUrl , ( message ) => {
14- const parsed : ChatMessage = JSON . parse ( message . body ) ;
15- onMessage ( parsed ) ;
16- } ) ;
33+ setupSubscription ( ) ;
1734
1835 return ( ) => {
19- subscription . unsubscribe ( ) ;
36+ if ( subscription ) {
37+ try {
38+ subscription . unsubscribe ( ) ;
39+ console . log ( '채팅 구독 해제' ) ;
40+ } catch ( error ) {
41+ console . error ( '구독 해제 중 오류 발생:' , error ) ;
42+ }
43+ }
2044 } ;
21- } , [ client , isConnected , onMessage ] ) ;
45+ } , [ client , isConnected , chatId , onMessage ] ) ;
2246
23- const sendMessage = ( content : string , attachments ?: { type : string ; url : string } [ ] ) => {
24- if ( ! client || ! isConnected ) return ;
47+ const sendMessage = useCallback ( async ( content : string , attachments ?: { type : string ; url : string } [ ] ) => {
48+ if ( ! client || ! isConnected || ! chatId ) {
49+ console . warn ( '메시지를 보낼 수 없습니다:' , {
50+ client : ! ! client ,
51+ isConnected,
52+ chatId
53+ } ) ;
54+ return Promise . reject ( new Error ( '연결 상태가 올바르지 않습니다.' ) ) ;
55+ }
2556
57+ const tempId = uuidv4 ( ) ;
2658 const sendUrl = `/pub/message/${ chatId } ` ;
27- const message = { chatId, content, attachments, createdAt : new Date ( ) . toISOString ( ) } ;
59+ const message = {
60+ chatId,
61+ content,
62+ attachments,
63+ createdAt : new Date ( ) . toISOString ( ) ,
64+ tempId
65+ } ;
2866
29- client . publish ( {
30- destination : sendUrl ,
31- body : JSON . stringify ( message ) ,
67+ return new Promise ( ( resolve , reject ) => {
68+ try {
69+ client . publish ( {
70+ destination : sendUrl ,
71+ body : JSON . stringify ( message ) ,
72+ } ) ;
73+ resolve ( tempId ) ;
74+ } catch ( error ) {
75+ console . error ( '메시지 전송 중 오류 발생:' , error ) ;
76+ reject ( error ) ;
77+ }
3278 } ) ;
33- } ;
79+ } , [ client , isConnected , chatId ] ) ;
3480
35- return { sendMessage } ;
81+ return { sendMessage, isConnected } ;
3682} ;
0 commit comments