0

I am trying to run my client without my server connected (on purpose) and catch ERR_CONNECTION_REFUSED error and display it to the user. I read here that this can be achieved using socket events, specifically connect_error

Below in my code, I can never get the events to fire and display the console logs inside. logging this.io.socket prints stuff but none of the events do.. why is that?

$.ajax(args)
        .done((msg) => {
          this.io.socket.on('connect', msg => {
            console.log('connect socket io', msg)
          })
          resolve(msg);
        })
        .fail((jqXHR, msg) => {
          return new Promise((resolve, reject) => {
            console.log('inside promise of fail() - this.io.socket', this.io.socket) // this will log data to console

            this.io.socket.on('connect_error', msg => {
              console.log('connect_error socket io', msg)
            })

            this.io.socket.on('connect_failed', (msg) => {
              console.log('connect_failed', msg);
            });

            // return some error here for user
          })
        });
asus
  • 1,427
  • 3
  • 25
  • 59

1 Answers1

0

From what I see, you are attempting to wire up the event handlers only if you get a bad response from your first ajax call. This will not result in any of the socket.io event handlers being initiated.

Move the event handler into the code where you initialize the socket instance.

See below for a full example where all the manager and socket events will be logged to console.

$.ajax(args)
    .done((msg) => {
        // connect to your server
        const socket = io('http://localhost:3000', {
            transports: ['websocket']
        });

        // manager events
        //_________________________________________________________________________________________________
        socket.io.on('connect_error', (err) => {
            console.error(`manager:connect_error ${err}`);
        });

        socket.io.on('connect_timeout', () => {
            console.error(`manager:connect_timeout`);
        });

        socket.io.on('reconnect_attempt', (attempt) => {
            console.error(`manager:reconnect_attempt ${attempt}`);
        });

        socket.io.on('reconnecting', (attempt) => {
            console.error(`manager:reconnecting ${attempt}`);
        });

        socket.io.on('reconnect_error', (err) => {
            console.error(`manager:reconnect_error ${err}`);
        });

        socket.io.on('reconnect_failed', () => {
            console.error(`manager:reconnect_failed`);
        });
        //_________________________________________________________________________________________________

        // socket events
        //_________________________________________________________________________________________________
        socket.on('connect', () => {
            console.log(`socket:connect ${socket.connected}`);
        });

        socket.on('connect_error', (err) => {
            console.error(`socket:connect_error ${err}`);
        });

        socket.on('connect_timeout', (timeout) => {
            console.error(`socket:connect_timeout ${timeout}`);
        });

        socket.on('error', (err) => {
            console.error(`socket:error ${err}`);
        });

        socket.on('disconnect', (reason) => {
            console.info(`socket:disconnect ${reason}`);
            if (reason === 'io server disconnect') {
                // the disconnection was initiated by the server, you need to reconnect manually
                socket.connect();
            }
            // else the socket will automatically try to reconnect
        });

        socket.on('reconnect', (attempt) => {
            console.error(`socket:reconnect ${attempt}`);
        });

        socket.on('reconnect_attempt', (attempt) => {
            console.error(`socket:reconnect_attempt ${attempt}`);
        });

        socket.on('reconnecting', (attempt) => {
            console.error(`socket:reconnecting ${attempt}`);
        });

        socket.on('reconnect_error', (err) => {
            console.error(`socket:reconnect_error ${err}`);
        });

        socket.on('reconnect_failed', () => {
            console.error(`socket:reconnect_failed`);
        });
        //_________________________________________________________________________________________________

        // custom events
        //_________________________________________________________________________________________________
        socket.on('hello', (message) => {
            console.info(message);

            socket.emit('hello', {foo: 'baz'});
        });
        //_________________________________________________________________________________________________

        resolve(msg);
    })
    .fail((jqXHR, msg) => {
        console.error(msg);
    });
Samuel Goldenbaum
  • 18,391
  • 17
  • 66
  • 104