File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -306,10 +306,12 @@ export default class ChainlinkDataStreamsConsumer extends EventEmitter {
306306 if ( this . ws ) this . disconnectImpl ( ) ;
307307 const ws = ( this . ws = new WebSocket ( url . toString ( ) , { headers } ) ) ;
308308 const onerror = ( error ) => {
309+ console . error ( 'Socket error:' , error )
309310 unbind ( ) ;
310311 resolve ( ) ;
311312 } ;
312313 const onopen = ( ) => {
314+ console . debug ( 'Socket opened.' )
313315 unbind ( ) ;
314316 // reset reconnect attempts on successful connection
315317 this . reconnect . attempts = 0 ;
@@ -320,6 +322,7 @@ export default class ChainlinkDataStreamsConsumer extends EventEmitter {
320322 ws . off ( 'open' , onopen ) ;
321323 } ;
322324 const onclose = ( ) => {
325+ console . debug ( 'Socket closed.' )
323326 unbind ( ) ;
324327 if ( ! this . reconnect ?. enabled ) {
325328 console . debug (
@@ -343,9 +346,7 @@ export default class ChainlinkDataStreamsConsumer extends EventEmitter {
343346 `Reconnecting attempt #${ this . reconnect . attempts } /${ this . reconnect . maxAttempts } ` ,
344347 `in ${ this . reconnect . interval } ms...` ,
345348 ) ;
346- setTimeout ( ( ) => {
347- this . connectImpl ( ) ;
348- } , this . reconnect . interval ) ;
349+ setTimeout ( this . connectImpl , this . reconnect . interval ) ;
349350 } else {
350351 const error =
351352 `Max reconnect attempts (${ this . reconnect . maxAttempts } ) reached. Giving up.`
Original file line number Diff line number Diff line change 11import ChainlinkDataStreamsConsumer , { Report } from './index.js' ;
22import assert from 'node:assert' ;
33import 'dotenv/config' ;
4+ import { WebSocket as _WebSocket } from 'ws' ;
5+ const WebSocket = _WebSocket || globalThis . WebSocket ;
46
57const DEBUG = false ;
68
@@ -369,6 +371,24 @@ describe('ChainlinkDataStreamsConsumer', function () {
369371 SDK . subscribeTo ( [ ] ) ;
370372 } ) ;
371373
374+ it ( 'should reconnect when socket closes' , async function ( done ) {
375+ this . timeout ( 10000 ) ;
376+ const SDK = new ChainlinkDataStreamsConsumer ( {
377+ ...config ( ) ,
378+ feeds : feedIds ,
379+ lazy : true ,
380+ } ) ;
381+ const connecting = await SDK . connect ( ) ;
382+ SDK . ws . close ( ) ;
383+ if ( SDK . ws . readyState === WebSocket . CONNECTING ) {
384+ ws . once ( 'open' , ( ) => {
385+ done ( ) ;
386+ } ) ;
387+ } else if ( ws . readyState === WebSocket . OPEN ) {
388+ done ( )
389+ }
390+ } ) ;
391+
372392 it ( 'should throw an error when calling Report.fromSocketMessage with invalid data' , function ( ) {
373393 assert . throws ( ( ) => Report . fromSocketMessage ( { } ) , {
374394 name : 'Error' ,
You can’t perform that action at this time.
0 commit comments