监听 websocket.readyState 的变化

  • CONNECTING:值为0,表示正在连接。
  • OPEN:值为1,表示连接成功,可以通信了。
  • CLOSING:值为2,表示连接正在关闭。
  • CLOSED:值为3,表示连接已经关闭,或者打开连接失败。
this.ws = new WebSocket("ws://ip:port/ws/muweb/channels/")

this.ws.addEventListener('open', () => { 
    console.log('socket 打开了')
    this.ws_status = this.ws.readyState;
});

this.ws.addEventListener('close', () => {
    console.log('socket 关闭了')
    this.ws_status = this.ws.readyState;
});
Logo

前往低代码交流专区

更多推荐