// import { showInfoMsg, showErrorMsg } from '@/utils/popInfo' // import ElementUI from 'element-ui'; import { Toast } from 'vant'; function initWebSocket(that,baseObj) { console.log(baseObj) console.log(this) console.log(that.socketObj) // return // const wsUri = WS_API + "/webSocket/" + e; // var wsUri ="ws://192.168.0.200:8005/qrCodePage/ID=1/refreshTime=5"; var wsUri = "ws://192.168.11.87:8088/level/gauge/1/2" let socketStr = 'socket'+1+'a'+'2'; that.socketObj[socketStr] = new WebSocket(wsUri)//这里面的this都指向vue // 监听socket连接 that.socketObj[socketStr].onopen = WebSocketOnopen; // 监听socket错误信息 that.socketObj[socketStr].onerror = webSocketOnError; // 监听socket消息 that.socketObj[socketStr].onmessage = webSocketOnMessage; // 监听socket关闭 that.socketObj[socketStr].onclose = closeWebsocket; } // this.$websocket.close();//全局 关闭 // this.$websocket.initWebSocket(xxx); //全局 吊起 // 监听socket连接 function WebSocketOnopen(e) { console.log("socket连接成功") Toast("socket连接成功" ) } // 监听socket错误信息 function webSocketOnError(e) { console.log(e) Toast("WebSocket连接发生错误" + e) } // 监听socket消息 function webSocketOnMessage(e) { console.log(e) let messObj=JSON.parse(e.data) let socketStr = 'socket'+1+'a'+'2'; if(messObj.code==500){ this.close(socketStr) Toast("断开了") }else{ Toast("接收到信息了") } // console.log('错误信息',e) // const data = JSON.parse(e.data); } // 关闭websiocket function closeWebsocket() { console.log('连接已关闭...') } function close(socketStr) { that.socketObj[socketStr].close() // 关闭 websocket that.socketObj[socketStr].onclose = function (e) { console.log(e)//监听关闭事件 console.log('关闭') } } function webSocketSend(agentData) { that.socketObj.socket.send(agentData); } export default { initWebSocket, close }
标签:function,WebSocket,socket,吊起,socketObj,socketStr,vues,console,log From: https://www.cnblogs.com/dianzan/p/17448513.html