Javascript doesn't catch error in WebSocket instantiation

后端 未结 2 983
名媛妹妹
名媛妹妹 2020-12-08 09:37

My socket currently throws net::ERR_CONNECTION_REFUSED because the server isn\'t running, which I want it to do at the moment.

The problem is that the following piec

2条回答
  •  星月不相逢
    2020-12-08 10:32

    I tried creating fiddle with it and I am able to see line printed.

    host='localhost';
    port=100;
    try {
    
        ws = new WebSocket('ws://'+ host + ':' + port + '/');
      }
      catch (err) {
        console.log('This never prints');
      }
      ws.onerror = function (error) {
        console.log(error);
      };
    

    https://jsfiddle.net/xzumgag0/

提交回复
热议问题