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

后端 未结 4 2033
难免孤独
难免孤独 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:26

    I ran into the same issue. There is a un-documented difference between namespace mode on and off that I also ran into, in the end I had to use the chrome debugger to work it out.

    // Bind to the news namespace, also get the underlying socket
    var ns_news = clientio.connect( 'http://localhost:4000/news' );
    var socket = ns_news.socket
    

    So, you see above that you have to get the actual socket ns_news.socket from your namespace if you want to bind "socket level" events such as connect failed, disconnect, etc

    // Global events are bound against socket
    socket.on('connect_failed', function(){
        console.log('Connection Failed');
    });
    socket.on('connect', function(){
        console.log('Connected');
    });
    socket.on('disconnect', function () {
      console.log('Disconnected');
    });
    
    
    // Your events are bound against your namespace(s)
    ns_news.on('myevent', function() {
        // Custom event code here
    });
    

    Note that you now use ns_news to send or receive events

    ns_news.send('hi there');
    

    Finally, be sure to bind the socket.on('error', ...) event as well - it occurs if the port wasn't available

    Full implimentation of 8080 connect with fallback to port 80

    I did this in my implementation (try port 8080, if that fails, try port 80 through nginx)

    socket_port = 8080;
    
    ns_dash = io.connect("http://your.gridspy.co.nz/dash", {
      port: socket_port,
      'connect timeout': 5000,
      'flash policy port': 10843
    });
    
    socket = ns_dash.socket;
    
    try_other_port = function() {
      if (socket_port !== 80) {
        if (typeof console !== "undefined" && console !== null) {
          console.log("Trying other port, instead of port " + socket_port + ", attempting port 80");
        }
        socket_port = 80;
        socket.options.port = socket_port;
        socket.options.transports = ['htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling'];
        return socket.connect();
      } else {
        return typeof console !== "undefined" && console !== null ? console.log("No other ports to try.") : void 0;
      }
    };
    socket.on('connect_failed', function() {
      if (typeof console !== "undefined" && console !== null) {
        console.log("Connect failed (port " + socket_port + ")");
      }
      return try_other_port();
    });
    socket.on('error', function() {
      if (typeof console !== "undefined" && console !== null) {
        console.log("Socket.io reported a generic error");
      }
      return try_other_port();
    });
    

    I also added an nginx proxy_pass rule, which looks like this

    location ~ ^/(socket.io)/ {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
    

    The Upgrade and Connection options here (from more recent versions of nginx) are required to upgrade the connection between nginx and socket.io to support websockets. That means that your webserver can support websockets on port 80.

    Furthermore if your server supports ssl / https, this will proxy wss which is the secure version of websockets.

提交回复
热议问题