signalrProvider.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. 'use client';
  2. import { createContext, useContext, useEffect, useState, useRef } from 'react';
  3. import * as signalR from '@microsoft/signalr';
  4. import { fetchApi, throwError } from '@/lib/utils/client';
  5. const SignalRContext = createContext<{
  6. cryptoConnection: signalR.HubConnection | null;
  7. chatConnection: signalR.HubConnection | null;
  8. cryptoConnected: boolean;
  9. chatConnected: boolean;
  10. stopConnections: () => Promise<void>;
  11. reconnectChat: (accessToken?: string | null) => Promise<void>;
  12. }>({
  13. cryptoConnection: null,
  14. chatConnection: null,
  15. cryptoConnected: false,
  16. chatConnected: false,
  17. stopConnections: async () => {},
  18. reconnectChat: async () => {}
  19. });
  20. type Props = {
  21. children: React.ReactNode;
  22. accessToken: string|null;
  23. signalRCryptoUrl: string;
  24. signalRChatUrl: string;
  25. }
  26. export function SignalRProvider({ children, accessToken, signalRCryptoUrl, signalRChatUrl }: Props) {
  27. const cryptoConnectionRef = useRef<signalR.HubConnection|null>(null);
  28. const chatConnectionRef = useRef<signalR.HubConnection|null>(null);
  29. const [chatConnection, setChatConnection] = useState<signalR.HubConnection|null>(null);
  30. const [cryptoConnected, setCryptoConnected] = useState<boolean>(false);
  31. const [chatConnected, setChatConnected] = useState<boolean>(false);
  32. // 초기 렌더 시에만 전달됨. 토큰 갱신 시에는 reconnectChat()을 통해 수동으로 재연결 처리
  33. useEffect(() => {
  34. initCryptoConnection();
  35. initChatConnection(accessToken);
  36. return () => {
  37. stopConnections();
  38. };
  39. }, []);
  40. useEffect(() => {
  41. if (cryptoConnected) {
  42. console.info('SignalR Crypto Connected');
  43. }
  44. }, [cryptoConnected]);
  45. useEffect(() => {
  46. if (chatConnected) {
  47. console.info('SignalR Chat Connected');
  48. }
  49. }, [chatConnected]);
  50. const initCryptoConnection = async () => {
  51. try {
  52. if (cryptoConnectionRef.current && cryptoConnectionRef.current.state !== signalR.HubConnectionState.Disconnected) {
  53. return;
  54. }
  55. const conn = new signalR.HubConnectionBuilder().withUrl(signalRCryptoUrl).withAutomaticReconnect().build();
  56. await conn.start();
  57. setCryptoConnected(true);
  58. cryptoConnectionRef.current = conn;
  59. } catch (error) {
  60. console.error('SignalR Crypto Connect Failed:', error);
  61. }
  62. };
  63. const initChatConnection = async (accessToken?: string|null) => {
  64. try {
  65. if (chatConnectionRef.current && chatConnectionRef.current.state !== signalR.HubConnectionState.Disconnected) {
  66. await chatConnectionRef.current.stop();
  67. }
  68. const connectionOptions = accessToken ? { accessTokenFactory: async () => accessToken, withCredentials: true } : {};
  69. const conn = new signalR.HubConnectionBuilder().withUrl(signalRChatUrl, connectionOptions).withAutomaticReconnect().build();
  70. await conn.start();
  71. conn.on('Connected', (message) => {
  72. console.info(message);
  73. });
  74. conn.on('Logout', (message) => {
  75. console.info(message);
  76. });
  77. conn.on('Kick', async () => {
  78. fetchApi('/api/auth/logout', {
  79. method: 'POST'
  80. }).then((res) => {
  81. throwError(res);
  82. alert('관리자에 의해 강제 종료되었습니다.');
  83. localStorage.setItem('rememberMe', "false");
  84. localStorage.removeItem('member');
  85. location.replace('/');
  86. });
  87. });
  88. chatConnectionRef.current = conn;
  89. setChatConnection(conn);
  90. setChatConnected(true);
  91. } catch (error) {
  92. console.error('SignalR Chat Connect Failed:', error);
  93. }
  94. };
  95. const stopConnections = async () => {
  96. if (chatConnectionRef.current && chatConnectionRef.current.state === signalR.HubConnectionState.Connected) {
  97. try {
  98. await chatConnectionRef.current.invoke('Logout');
  99. setChatConnected(false);
  100. } catch (error) {
  101. console.error('SignalR Chat Disconnect Failed:', error);
  102. }
  103. }
  104. if (cryptoConnectionRef.current && cryptoConnectionRef.current.state === signalR.HubConnectionState.Connected) {
  105. try {
  106. await cryptoConnectionRef.current.stop();
  107. setCryptoConnected(false);
  108. } catch (error) {
  109. console.error('SignalR Crypto Disconnect Failed:', error);
  110. }
  111. }
  112. };
  113. const reconnectChat = async (token?: string | null) => {
  114. await initChatConnection(token);
  115. };
  116. return (
  117. <SignalRContext.Provider value={{
  118. cryptoConnection: cryptoConnectionRef.current,
  119. chatConnection,
  120. cryptoConnected,
  121. chatConnected,
  122. stopConnections,
  123. reconnectChat
  124. }}>
  125. {children}
  126. </SignalRContext.Provider>
  127. )
  128. }
  129. export function useSignalRContext() {
  130. return useContext(SignalRContext);
  131. }