简体   繁体   中英

socket.io client catch error : " WebSocket connection to failed: Error in connection establishment:" object

I am trying to catch connection failed error if the given host is not correct or cannot be reached.but it seems this error is logging from websocket itself and is not related to shocket io it self.

WebSocket connection to 'ws://file/socket.io/?EIO=4&transport=websocket' failed: Error in connection establishment:.net::ERR_NAME_NOT_RESOLVED

I tried this catch errors naming events:

        socket.on("connect_error", (error)=>{
            console.log(error)
        })
        socket.on("connect_failed", (error)=>{
            console.log(error)
        })
        socket.on("error", (error)=>{
            console.log(error)
        })

but none of them are working.

research:

https://github.com/socketio/socket.io-client/issues/1097

I tried this option on my socketio client instance:

const socket = require('socket.io-client')('https://somefoo.com', {
  transports: ['websocket'],
  rejectUnauthorized: false
})

What you can try to do, is to use the socket.io manager which is catching errors of a higher level.

https://socket.io/docs/v3/client-api/#Event-%E2%80%98error%E2%80%99

https://socket.io/docs/v3/client-api/#Manager

    const manager = new Manager('https://somefoo.com')
    const socket = manager.socket('/')

    manager.on('error', (error) => {
        // Fired upon a connection error.
        console.log('socket error')
    })

    socket.on('connect_error', (error) => {
        // Fired when an namespace middleware error occurs.
        console.log('connection error')
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM