关于websocket通讯

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')
    }
  }
}

  

posted @ 2017-06-06 09:43  青草圆  阅读(220)  评论(0编辑  收藏  举报