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