@@ -49,7 +49,7 @@ export const useChartSocket = ({
4949 retryOnError = true ,
5050 reconnectAttempts,
5151 reconnectInterval,
52- wsUrl : socketUrl ,
52+ wsUrl,
5353 socketUrl : socketIOUrl ,
5454 socketType = "websocket" ,
5555 pairMapping = PAIRS as unknown as PairToken [ ] ,
@@ -59,10 +59,10 @@ export const useChartSocket = ({
5959
6060 // WebSocket implementation
6161 const { lastJsonMessage, sendJsonMessage } = useWebSocket < LastJsonMessageType > (
62- socketType === "websocket" ? socketUrl || WS_URL : null ,
62+ socketType === "websocket" ? wsUrl || WS_URL : null ,
6363 {
6464 onOpen : ( ) => {
65- console . info ( "useChartSocket: connect WebSocket - " , socketUrl ) ;
65+ console . info ( "useChartSocket: connect WebSocket - " , wsUrl ) ;
6666 setIsConnected ( true ) ;
6767 } ,
6868 onClose : ( ) => {
@@ -75,76 +75,86 @@ export const useChartSocket = ({
7575 } ,
7676 shouldReconnect : ( ) => true ,
7777 onError : ( error ) => {
78- if ( ! socketUrl ) {
78+ if ( ! wsUrl ) {
7979 console . warn ( "useChartSocket: Not have socketUrl option in websocket!" , JSON . stringify ( error ) ) ;
8080 return ;
8181 }
8282 console . error ( "useChartSocket: Have something went wrong with connection." , JSON . stringify ( error ) ) ;
8383 } ,
84- reconnectAttempts : ! socketUrl ? 0 : reconnectAttempts || WEBSOCKET_RECONNECT_ATTEMPTS ,
85- reconnectInterval : ! socketUrl ? 0 : reconnectInterval || WEBSOCKET_RECONNECT_INTERVAL ,
84+ reconnectAttempts : ! wsUrl ? 0 : reconnectAttempts || WEBSOCKET_RECONNECT_ATTEMPTS ,
85+ reconnectInterval : ! wsUrl ? 0 : reconnectInterval || WEBSOCKET_RECONNECT_INTERVAL ,
8686 retryOnError : ! ! retryOnError
8787 }
8888 ) ;
8989
90- // Socket.IO implementation
91- useEffect ( ( ) => {
92- if ( socketType === "socketio" && socketIOUrl ) {
93- const ohlcvHandler = ( payload : any ) => {
94- try {
95- const event =
96- Array . isArray ( payload ) && payload . length === 2 && typeof payload [ 0 ] === "string" ? payload [ 1 ] : payload ;
90+ const ohlcvHandler = ( payload : any ) => {
91+ try {
92+ const event =
93+ Array . isArray ( payload ) && payload . length === 2 && typeof payload [ 0 ] === "string" ? payload [ 1 ] : payload ;
9794
98- console . log ( "event" , { event, payload } ) ;
95+ console . log ( "event" , { event, payload } ) ;
9996
100- if ( ! event ) return ;
97+ if ( ! event ) return ;
10198
102- const { tokenMint, open, high, low, close, volume, minute } = event ;
99+ const { tokenMint, open, high, low, close, volume, minute } = event ;
103100
104- // Try to find the mapped pair
105- const mapped = ( pairMapping || [ ] ) . find ( ( p : any ) => {
106- // ADL story pairs expose `to` and `info` fields
107- return (
108- ( p as any ) ?. to === tokenMint ||
109- ( typeof ( p as any ) ?. info === "string" && ( p as any ) . info . endsWith ( tokenMint ) ) ||
110- ( typeof ( p as any ) ?. id === "string" && ( p as any ) . id . includes ( tokenMint ) )
111- ) ;
112- } ) ;
101+ // Try to find the mapped pair
102+ const mapped = ( pairMapping || [ ] ) . find ( ( p : any ) => {
103+ // ADL story pairs expose `to` and `info` fields
104+ return (
105+ ( p as any ) ?. to === tokenMint ||
106+ ( typeof ( p as any ) ?. info === "string" && ( p as any ) . info . endsWith ( tokenMint ) ) ||
107+ ( typeof ( p as any ) ?. id === "string" && ( p as any ) . id . includes ( tokenMint ) )
108+ ) ;
109+ } ) ;
113110
114- // Only handle if it matches current active pair (when we can determine it)
115- const mappedInfo = ( mapped as any ) ?. info as string | undefined ;
116- const isActivePair = mappedInfo ? mappedInfo === pairActive . info : true ;
117-
118- if ( ! isActivePair ) return ;
119-
120- const timeInSeconds = typeof minute === "number" ? minute * 60 : undefined ;
121- if ( ! timeInSeconds ) return ;
122-
123- const tradeData = {
124- open,
125- high,
126- low,
127- close,
128- volume,
129- time : timeInSeconds ,
130- pair : mappedInfo || pairActive . info
131- } as any ;
132-
133- setData ( tradeData ) ;
134- handleTradeEvent ( tradeData , pairMapping ) ;
135- } catch ( err ) {
136- console . error ( "useChartSocket: error handling updateOhlcv" , err ) ;
137- }
138- } ;
111+ // Only handle if it matches current active pair (when we can determine it)
112+ const mappedInfo = ( mapped as any ) ?. info as string | undefined ;
113+ const isActivePair = mappedInfo ? mappedInfo === pairActive . info : true ;
114+
115+ if ( ! isActivePair ) return ;
116+
117+ const timeInSeconds = typeof minute === "number" ? minute * 60 : undefined ;
118+ if ( ! timeInSeconds ) return ;
119+
120+ const tradeData = {
121+ open,
122+ high,
123+ low,
124+ close,
125+ volume,
126+ time : timeInSeconds ,
127+ pair : mappedInfo || pairActive . info
128+ } as any ;
129+
130+ setData ( tradeData ) ;
131+ handleTradeEvent ( tradeData , pairMapping ) ;
132+ } catch ( err ) {
133+ console . error ( "useChartSocket: error handling updateOhlcv" , err ) ;
134+ }
135+ } ;
136+
137+ // Socket.IO implementation
138+ useEffect ( ( ) => {
139+ if ( socketType === "socketio" && socketIOUrl ) {
139140 const initSocketIO = async ( ) => {
140141 try {
142+ // Clean up existing socket if any
143+ if ( socketIORef . current ) {
144+ socketIORef . current . off ( eventName || "updateOhlcv" , ohlcvHandler ) ;
145+ socketIORef . current . disconnect ( ) ;
146+ socketIORef . current = null ;
147+ }
148+
141149 const socket = io ( socketIOUrl , {
142- transports : [ "websocket" ] , // " polling"
143- autoConnect : true ,
150+ transports : [ "websocket" , "polling" ] , // Fallback to polling if websocket fails
151+ autoConnect : false , // Don't auto connect, we'll connect manually
144152 reconnection : true ,
145153 reconnectionAttempts : reconnectAttempts || WEBSOCKET_RECONNECT_ATTEMPTS ,
146154 reconnectionDelay : reconnectInterval || WEBSOCKET_RECONNECT_INTERVAL ,
147155 reconnectionDelayMax : 5000 ,
156+ timeout : 20000 , // Increase timeout
157+ forceNew : true , // Force new connection
148158 ...socketIOOptions
149159 } ) ;
150160
@@ -153,13 +163,14 @@ export const useChartSocket = ({
153163 setIsConnected ( true ) ;
154164 } ) ;
155165
156- socket . on ( "disconnect" , ( ) => {
157- console . info ( "useChartSocket: Socket.IO disconnected" ) ;
166+ socket . on ( "disconnect" , ( reason ) => {
167+ console . info ( "useChartSocket: Socket.IO disconnected" , reason ) ;
158168 setIsConnected ( false ) ;
159169 } ) ;
160170
161171 socket . on ( "connect_error" , ( error ) => {
162172 console . error ( "useChartSocket: Socket.IO connection error" , error ) ;
173+ setIsConnected ( false ) ;
163174 } ) ;
164175
165176 socket . on ( "reconnect_attempt" , ( attemptNumber ) => {
@@ -168,38 +179,42 @@ export const useChartSocket = ({
168179
169180 socket . on ( "reconnect_failed" , ( ) => {
170181 console . error ( "useChartSocket: Socket.IO reconnection failed" ) ;
182+ setIsConnected ( false ) ;
183+ } ) ;
184+
185+ socket . on ( "error" , ( error ) => {
186+ console . error ( "useChartSocket: Socket.IO error" , error ) ;
171187 } ) ;
172188
173189 // Listen for ADL updateOhlcv events
174190 socket . on ( eventName || "updateOhlcv" , ohlcvHandler ) ;
175191
176192 socketIORef . current = socket ;
193+
194+ // Connect manually after setting up all event handlers
195+ socket . connect ( ) ;
177196 } catch ( error ) {
178197 console . error ( "useChartSocket: Failed to initialize Socket.IO" , error ) ;
198+ setIsConnected ( false ) ;
179199 }
180200 } ;
181201
182202 initSocketIO ( ) ;
183203
184204 return ( ) => {
185205 if ( socketIORef . current ) {
186- socketIORef . current . off ( eventName || "updateOhlcv" , ohlcvHandler ) ;
187- socketIORef . current . disconnect ( ) ;
188- socketIORef . current = null ;
206+ try {
207+ socketIORef . current . off ( eventName || "updateOhlcv" , ohlcvHandler ) ;
208+ socketIORef . current . disconnect ( ) ;
209+ socketIORef . current = null ;
210+ setIsConnected ( false ) ;
211+ } catch ( error ) {
212+ console . error ( "useChartSocket: Error during cleanup" , error ) ;
213+ }
189214 }
190215 } ;
191216 }
192- } , [
193- socketType ,
194- socketIOUrl ,
195- currentPair . info ,
196- period ,
197- pairMapping ,
198- reconnectAttempts ,
199- reconnectInterval ,
200- socketIOOptions ,
201- pairActive . info
202- ] ) ;
217+ } , [ socketType , socketIOUrl , eventName , reconnectAttempts , reconnectInterval , socketIOOptions ] ) ;
203218
204219 // WebSocket subscription logic
205220 useEffect ( ( ) => {
@@ -237,36 +252,6 @@ export const useChartSocket = ({
237252 }
238253 } , [ sendJsonMessage , currentPair , period , socketType ] ) ;
239254
240- // // Socket.IO subscription logic
241- // useEffect(() => {
242- // if (socketType === "socketio" && socketIORef.current && isConnected && currentPair && period) {
243- // if (period !== currentPeriod || currentPair.info !== pairActive?.info) {
244- // // Unsubscribe from previous pair/period
245- // socketIORef.current.emit("unsubscribe", {
246- // stream: `${pairActive.info}@${currentPeriod}`
247- // });
248-
249- // setPeriod(period);
250- // setPairActive(currentPair);
251- // }
252-
253- // console.info("Socket.IO SUBSCRIBE", {
254- // stream: `${currentPair.info}@${period}`
255- // });
256-
257- // // Subscribe to new pair/period
258- // socketIORef.current.emit("subscribe", {
259- // stream: `${currentPair.info}@${period}`
260- // });
261-
262- // return () => {
263- // socketIORef.current.emit("unsubscribe", {
264- // stream: `${currentPair.info}@${period}`
265- // });
266- // };
267- // }
268- // }, [socketType, isConnected, currentPair, period, currentPeriod, pairActive]);
269-
270255 // Handle WebSocket messages
271256 useEffect ( ( ) => {
272257 if ( socketType === "websocket" && lastJsonMessage && lastJsonMessage . data ) {
0 commit comments