Node.js socket.io-client connect_failed / connect_error event

后端 未结 4 2041
难免孤独
难免孤独 2020-12-05 07:40

I am playing around with node.js and socket.io-client. I am trying to connect to a channel that does not exist in order to trigger the event \'connect_failed\' (as specified

4条回答
  •  悲&欢浪女
    2020-12-05 08:23

    This applies to version 1.4.5 (might work on previous versions also).

    What you want to do is listen for the "error" event on the socket.io client. It will trigger this event, and the error message will be "Invalid namespace".

    Here's an example of how to handle it from the socket.io client:

      var ioclient = require('socket.io-client');
      var websocket = ioclient('http://myserverurl.com/namespacethatdoesntexist');
    
      websocket.on('error', function(err){
        if (err == 'Invalid namespace') {
          console.warn("Attempted to connect to invalid namespace");
          //handle the case here...
        } else {
          console.warn("Error on socket.io client", err);
        }
      });
    

提交回复
热议问题