@@ -433,11 +433,26 @@ export class Socket extends EventSpewer {
433433 } ; break ;
434434 }
435435
436- this . socket = new BaseSocket ( this . url . href ) ;
437- this . socket . on ( SocketEventsBase . CLOSE , this . onClose . bind ( this , this . socket ) ) ;
438- this . socket . on ( SocketEventsBase . ERROR , this . onError . bind ( this , this . socket ) ) ;
439- this . socket . on ( SocketEventsBase . MESSAGE , this . onMessage . bind ( this , this . socket ) ) ;
440- this . socket . on ( SocketEventsBase . OPEN , this . onOpen . bind ( this , this . socket ) ) ;
436+ try {
437+ this . socket = new BaseSocket ( this . url . href ) ;
438+ this . socket . on ( SocketEventsBase . CLOSE , this . onClose . bind ( this , this . socket ) ) ;
439+ this . socket . on ( SocketEventsBase . ERROR , this . onError . bind ( this , this . socket ) ) ;
440+ this . socket . on ( SocketEventsBase . MESSAGE , this . onMessage . bind ( this , this . socket ) ) ;
441+ this . socket . on ( SocketEventsBase . OPEN , this . onOpen . bind ( this , this . socket ) ) ;
442+ } catch ( error ) {
443+ this . socket = null ;
444+ if ( this . autoReconnect && ! this . killed ) {
445+ if ( this . reconnectMax < this . reconnects ) {
446+ this . kill ( new Error ( `Tried reconnecting more than ${ this . reconnectMax } times.` ) ) ;
447+ } else {
448+ this . emit ( SocketEvents . RECONNECTING ) ;
449+ setTimeout ( ( ) => {
450+ this . connect ( url ) ;
451+ this . reconnects ++ ;
452+ } , this . reconnectDelay ) ;
453+ }
454+ }
455+ }
441456
442457 this . setState ( SocketStates . CONNECTING ) ;
443458 this . emit ( SocketEvents . SOCKET , this . socket ) ;
0 commit comments