var ws = {
  init:function(callback){
    var _this = this;
    _this.callback = callback;
  },
  websocket:function(){
    var _this = this;
    if('WebSocket' in window){
      var websocket = new WebSocket('ws://192.168.2.170:7059');
      websocket.onopen = function(){
        console.log('打开websocket连接');
      }
      websocket.onmessage = function(event){
        console.log('接收服务端发送过来的信息')
        console.log(event);
        _this.callback && _this.callback(event)
      }
      websocket.onerror = function(){
        console.log('websocket连接发生错误');
      }
      websocket.onclose = function(){
        console.log('连接关闭');
      }
    }else {
      alert('当前浏览器不支持WebSocket')
    }
  }
}

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
  • 2021-09-20
  • 2021-11-10
  • 2022-12-23
  • 2021-05-27
  • 2021-12-19
猜你喜欢
  • 2021-07-12
  • 2021-09-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-28
  • 2021-03-10
相关资源
相似解决方案