From 0ddeddeaee4d59e9adfbfdeaac29d1a8d21422c8 Mon Sep 17 00:00:00 2001 From: z_yu <1534695664@qq.com> Date: Mon, 1 Aug 2022 16:38:14 +0800 Subject: [PATCH] websocket --- src/views/components/WarehouseWarning.vue | 126 +++++++++++++++++++- src/views/storeManage/warehouse3D/index.vue | 3 +- 2 files changed, 124 insertions(+), 5 deletions(-) diff --git a/src/views/components/WarehouseWarning.vue b/src/views/components/WarehouseWarning.vue index c59447e..ac27b51 100644 --- a/src/views/components/WarehouseWarning.vue +++ b/src/views/components/WarehouseWarning.vue @@ -31,9 +31,13 @@ export default { }, data() { return { - tableData: [] + tableData: [], + timeout: 1000 } }, + created() { + this.initWebSocket() + }, mounted() { this.getData() console.log(11333) @@ -104,15 +108,129 @@ export default { } } }, 1000 * 3 * this.scollNum) + }, + initWebSocket() { + const wsUri = process.env.VUE_APP_WS_API + '/webSocket' + this.websock = new WebSocket(wsUri) + this.websock.onerror = this.webSocketOnError + this.websock.onmessage = this.webSocketOnMessage + }, + webSocketOnError(e) { + this.$notify({ + title: 'WebSocket连接发生错误', + type: 'error', + duration: 0 + }) + }, + webSocketOnMessage(e) { + const data = JSON.parse(e.data) + console.log(data) + if (data.msgType === 'INFO') { + this.$notify({ + title: '', + message: data.msg, + type: 'success', + dangerouslyUseHTMLString: true, + duration: 5500 + }) + } else if (data.msgType === 'ERROR') { + this.$notify({ + title: '', + message: data.msg, + dangerouslyUseHTMLString: true, + type: 'error', + duration: 0 + }) + } + }, + webSocketSend(agentData) { + this.websock.send(agentData) } + // initWebSocket() { + // // if (WebSocket in window) { + // // 初始化weosocket + // const wsUri = process.env.VUE_APP_WS_API + '/webSocket' + // this.websocket = null + // this.websocket = new WebSocket(wsUri) + // this.websocket.onopen = this.websocketonopen + // this.websocket.onerror = this.websocketonerror + // this.websocket.onmessage = this.websocketonmessage + // this.websocket.onclose = this.websocketclose + // // } else { + // // // 浏览器不支持 WebSocket + // // console.log('您的浏览器不支持 WebSocket!') + // // } + // }, + // websocketonopen() { + // this.start() + // }, + // start() { + // // 开启心跳 + // const that = this + // that.timeoutObj && clearTimeout(that.timeoutObj) + // that.timeoutObj = setTimeout(function() { + // // 这里发送一个心跳,后端收到后,返回一个心跳消息, + // if (that.websocket && that.websocket.readyState === 1) { + // // 如果连接正常 + // console.log('WebSocket连接成功') + // that.websocket.send('----timingHeart---') + // } else { // 否则重连 + // that.reconnect() + // } + // }, that.timeout) + // }, + // reconnect() { // 重新连接 + // console.log('重新连接') + // const that = this + // if (that.websocket && that.websocket.readyState === 1) { + // clearInterval(that.timeoutnum) + // that.timeoutnum = null + // that.timeNum = 0 + // return + // } + // if (!that.timeoutnum) { + // that.timeoutnum = setInterval(function() { + // if (that.websocket && that.websocket.readyState === 1) { + // clearInterval(that.timeoutnum) + // that.timeoutnum = null + // that.timeNum = 0 + // return + // } + // // 新连接 + // that.initWebSocket() + // that.timeNum++ + // if (that.timeNum >= 3) { + // clearInterval(that.timeoutnum) + // that.timeoutnum = null + // that.timeNum = 0 + // } + // }, 1000) + // } + // }, + // websocketonerror(e) { + // // 错误 + // // this.initWebSocket() + // console.log('WebSocket连接发生错误') + // }, + // websocketonmessage(e) { + // // 数据接收 + // var data = JSON.parse(e.data) + // // this.$store.commit('SET_ws', data) + // console.log('接收数据', data) + // // 操作处理... + // }, + // websocketclose(e) { + // // 关闭 + // this.websocket && this.websocket.close && this.websocket.close() + // } } }