|
| 1 | +// 1. SSEProvider.tsx (Context + Provider) |
| 2 | +import { pushNotification } from '@service/feature/noti/hook/useSSE'; |
| 3 | +import React, { createContext, useContext, useEffect, useState } from 'react'; |
| 4 | +import { useDispatch } from 'react-redux'; |
| 5 | +import { useSelector } from 'react-redux'; |
| 6 | +import { toast } from 'sonner'; |
| 7 | +import { RootState } from 'src/app/store'; |
| 8 | +import { SSEMentionResponse, SSEResponse } from '../type/alert'; |
| 9 | +import Alarm from '@components/common/Alarm'; |
| 10 | +import { channel } from 'diagnostics_channel'; |
| 11 | + |
| 12 | +const SSEContext = createContext<{ events: MessageEvent[] }>({ events: [] }); |
| 13 | + |
| 14 | +export const SSEProvider = ({ children }: { children: React.ReactNode }) => { |
| 15 | + const [events, setEvents] = useState<MessageEvent[]>([]); |
| 16 | + const user = useSelector((state: RootState) => state.auth.user); |
| 17 | + const dispatch = useDispatch(); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const eventSource = new EventSource( |
| 21 | + `http://flowchat.shop:30100/sse/subscribe?memberId=${user?.userId}`, |
| 22 | + ); |
| 23 | + console.log('eventSource: ', eventSource); |
| 24 | + |
| 25 | + eventSource.addEventListener('friendRequestNotification', (event) => { |
| 26 | + console.log('[friendRequestNotification] event: ', event); |
| 27 | + const data: SSEResponse = JSON.parse(event.data); |
| 28 | + console.log('[SSE] data: ,', data); |
| 29 | + |
| 30 | + toast( |
| 31 | + <Alarm sender={data.sender} message={`친구 요청을 보냈습니다.`} />, |
| 32 | + { |
| 33 | + style: { |
| 34 | + padding: '12px', |
| 35 | + width: '220px', |
| 36 | + background: '#2e3036', |
| 37 | + borderRadius: '2px', |
| 38 | + boxShadow: '0px 0px 41px 0px rgba(0, 0, 0, 0.34)', |
| 39 | + border: '1px solid #42454A', |
| 40 | + }, |
| 41 | + }, |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + eventSource.addEventListener('friendAcceptNotification', (event) => { |
| 46 | + console.log('[friendAcceptNotification] event: ', event); |
| 47 | + const data: SSEResponse = JSON.parse(event.data); |
| 48 | + console.log('[SSE] data: ,', data); |
| 49 | + |
| 50 | + toast( |
| 51 | + <Alarm sender={data.sender} message={`친구 요청을 승낙하였습니다.`} />, |
| 52 | + { |
| 53 | + style: { |
| 54 | + padding: '12px', |
| 55 | + width: '220px', |
| 56 | + background: '#2e3036', |
| 57 | + borderRadius: '2px', |
| 58 | + boxShadow: '0px 0px 41px 0px rgba(0, 0, 0, 0.34)', |
| 59 | + border: '1px solid #42454A', |
| 60 | + }, |
| 61 | + }, |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + eventSource.addEventListener('mention', (event) => { |
| 66 | + console.log('[mention] event: ', event); |
| 67 | + const data: SSEMentionResponse = JSON.parse(event.data); |
| 68 | + console.log('[SSE] data: ,', data); |
| 69 | + |
| 70 | + toast( |
| 71 | + <Alarm |
| 72 | + sender={data.sender} |
| 73 | + message={`${data.content}`} |
| 74 | + channel={data.channel} |
| 75 | + team={data.team} |
| 76 | + category={data.category} |
| 77 | + chatId={data.chatId} |
| 78 | + />, |
| 79 | + { |
| 80 | + style: { |
| 81 | + padding: '12px', |
| 82 | + width: '220px', |
| 83 | + background: '#2e3036', |
| 84 | + borderRadius: '2px', |
| 85 | + boxShadow: '0px 0px 41px 0px rgba(0, 0, 0, 0.34)', |
| 86 | + border: '1px solid #42454A', |
| 87 | + }, |
| 88 | + }, |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + eventSource.onmessage = (event) => { |
| 93 | + console.log('[SSE] 도착. event: ', event); |
| 94 | + // setEvents((prev) => [...prev, event]); |
| 95 | + const data = JSON.parse(event.data); |
| 96 | + console.log('[SSE] data: ,', data); |
| 97 | + }; |
| 98 | + |
| 99 | + eventSource.onerror = () => { |
| 100 | + console.log('[SSE] 에러 발생. SSE 종료.'); |
| 101 | + eventSource.close(); |
| 102 | + }; |
| 103 | + |
| 104 | + return () => { |
| 105 | + eventSource.close(); |
| 106 | + }; |
| 107 | + }, []); |
| 108 | + |
| 109 | + return ( |
| 110 | + <SSEContext.Provider value={{ events }}>{children}</SSEContext.Provider> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +export const useSSE = () => useContext(SSEContext); |
0 commit comments