3

我们正在尝试WebSocket使用以下代码进行连接,当互联网正常工作时没有发现任何问题。但是当互联网关闭或由于其他一些与 N/W 相关的问题时,此消息将显示在浏览器控制台中:

“与 'wss://phone.company.com:5063/' 的 WebSocket 连接失败:连接建立错误:net::ERR_INTERNET_DISCONNECTED”。

有没有办法捕捉到这个错误并向客户抛出委托回调onWebSocketConnectionFailed()

var sConn = {
        socket: null,
        uri: 'wss://phone.complany.com:5063',

  try {

    this.ws = new WebSocket(this.url, 'sip');

    this.ws.onopen    = onOpen.bind(this);
    this.ws.onclose   = onClose.bind(this);
    this.ws.onmessage = onMessage.bind(this);
    this.ws.onerror   = onError.bind(this);

  } catch(e) {
    onError.call(this, e);
  }
}
4

0 回答 0