From 7e9629c4ddd855fda8a82043a642bbee4710a676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=A5=A5=E7=91=9E?= <1131549607@qq.com> Date: Thu, 24 Jul 2025 09:39:14 +0800 Subject: [PATCH] =?UTF-8?q?7.24=E5=BC=A0=E7=A5=A5=E7=91=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- env/.env.development | 4 +- src/common/socket.ts | 270 ++++++++++-------- .../device-control/office/ALLleader.vue | 140 +++------ .../device-control/office/Personaldevices.vue | 185 ++++++++++-- .../device-control/office/index.vue | 6 +- src/pages-message/chat/chat.vue | 1 + 6 files changed, 348 insertions(+), 258 deletions(-) diff --git a/env/.env.development b/env/.env.development index f347345..8293b47 100644 --- a/env/.env.development +++ b/env/.env.development @@ -7,7 +7,9 @@ VITE_SHOW_SOURCEMAP = true # 是否启用读取配置文件 min #VITE_WEBAPP = 'D://opt//AppUpdateTest' VITE_WEBAPP = '' -VITE_SERVER_BASEURL = 'http://10.75.15.249:8080/jeecg-boot' +VITE_SERVER_BASEURL = 'http://10.75.15.246:8080/jeecg-boot' +#websocket连接url-测试用 +VITE_WEBSOCKET_URL = 'ws://10.75.15.246:8899/mqttprod/ws/simple' #VITE_SERVER_BASEURL = 'https://36.112.48.190/jeecg-boot' #VITE_SERVER_BASEURL = 'https://szcx.zyyt.sinopec.com/jeecg-boot' diff --git a/src/common/socket.ts b/src/common/socket.ts index 0b52a34..facb619 100644 --- a/src/common/socket.ts +++ b/src/common/socket.ts @@ -1,140 +1,156 @@ // @ts-nocheck +import { log } from 'console' import { randomString } from './uitls' import { useUserStore } from '@/store/user' const baseUrl = import.meta.env.VITE_SERVER_BASEURL class socket { - constructor() { - this.socketUrl = baseUrl - this.socketStart = false - this.socketType = '' - this.monitorSocketError() - this.monitorSocketClose() - this.socketReceive() - } - init(socket_type, callback?) { + constructor() { + console.log(77777777777777) + this.socketUrl = baseUrl + this.socketStart = false + this.socketType = '' + this.monitorSocketError() + this.monitorSocketClose() + this.socketReceive() + } + init(socket_type, callback ?) { const userStore = useUserStore() - const _this = this - if (baseUrl) { - if (this.socketStart) { - console.log('webSocket已经启动了') - } else { - _this.socketType = socket_type - let url = - this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') + - '/' + - socket_type + - '/' + - userStore.userInfo.userid + - '_app' - if (socket_type == 'eoaNewChatSocket') { - let randomMessageId = randomString(6) - url = - this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') + - '/eoaNewChatSocket/' + - userStore.userInfo.userid + - '/' + - randomMessageId - } - console.log('启动this.socketUrl连接地址:', url) - // update-begin-author:taoyan date:20220422 for:v2.4.6 的 websocket 服务端,存在性能和安全问题。 #3278 + const _this = this + if (baseUrl) { + if (this.socketStart) { + console.log('webSocket已经启动了') + } else { + _this.socketType = socket_type + let url = + this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') + + '/' + + socket_type + + '/' + + userStore.userInfo.userid + + '_app' + if (socket_type == 'eoaNewChatSocket') { + let randomMessageId = randomString(6) + url = + this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') + + '/eoaNewChatSocket/' + + userStore.userInfo.userid + + '/' + + randomMessageId + } + console.log('启动this.socketUrl连接地址:', url) + // update-begin-author:taoyan date:20220422 for:v2.4.6 的 websocket 服务端,存在性能和安全问题。 #3278 - let token = userStore.userInfo.token - uni.connectSocket({ - url: url, - method: 'GET', - protocols: [token], - }) - // update-end-author:taoyan date:20220422 for: v2.4.6 的 websocket 服务端,存在性能和安全问题。 #3278 - uni.onSocketOpen((res) => { - this.socketStart = true - callback && callback() - console.log('WebSocket连接已打开!') - }) - /* setTimeout(() => { - _this.getHeartbeat(); - }, 5000); */ - } - } else { - console.log('config/baseUrl socketUrl为空') - } - } - // Socket给服务器发送消息 - send(data, callback) { + let token = userStore.userInfo.token + uni.connectSocket({ + url: url, + method: 'GET', + protocols: [token], + }) + // update-end-author:taoyan date:20220422 for: v2.4.6 的 websocket 服务端,存在性能和安全问题。 #3278 + uni.onSocketOpen((res) => { + console.log("连接进来了--------53333333333323----") + this.socketStart = true + callback && callback() + console.log('WebSocket连接已打开!') + }) + /* setTimeout(() => { + _this.getHeartbeat(); + }, 5000); */ + } + } else { + console.log('config/baseUrl socketUrl为空') + } + } + // Socket给服务器发送消息 + send(data, callback) { const userStore = useUserStore() - const _this = this - if (userStore.userInfo.userid) { - data.userUid = userStore.userInfo.userid - } - console.log(data) - uni.sendSocketMessage({ - data: JSON.stringify(data), - success: () => { - callback && callback(true) - }, - fail: () => { - callback && callback(false) - }, - }) - } - // Socket接收服务器发送过来的消息 - socketReceive() { - const _this = this - uni.onSocketMessage(function (res) { - console.log('APP:--》收到服务器内容:') - let data = JSON.parse(res.data) - // console.log('收到服务器内容:', data); - _this.acceptMessage && _this.acceptMessage(data) - }) - } - // 关闭Socket - closeSocket() { - const _this = this - uni.closeSocket() - _this.socketStart = false - } - // 监听Socket关闭 - monitorSocketClose() { - const _this = this - uni.onSocketClose(function (res) { - console.log('WebSocket 已关闭!') - _this.socketStart = false - setTimeout(function () { - _this.init(_this.socketType) - }, 3000) - }) - } - // 监听Socket错误 - monitorSocketError() { - const _this = this - uni.onSocketError(function (res) { - _this.socketStart = false - console.log('WebSocket连接打开失败,请检查!') - }) - } - // 心跳 - getHeartbeat() { + const _this = this + if (userStore.userInfo.userid) { + data.userUid = userStore.userInfo.userid + } + console.log(data) + uni.sendSocketMessage({ + data: JSON.stringify(data), + success: () => { + callback && callback(true) + }, + fail: () => { + callback && callback(false) + }, + }) + } + // Socket接收服务器发送过来的消息 + socketReceive() { + const _this = this + uni.onSocketMessage(function (res) { + console.log('APP:--》收到服务器内容:', res) + console.log('连接路径----:', baseUrl) + if (res.data.startsWith('CONNECTED') || res.data.startsWith('MESSAGE') || res.data.startsWith('RECEIPT')) { + console.log('放行----------:', res) + // 放行 STOMP 协议消息,交给 socketTask.onMessage 处理 by 闵 + return; + } + let data = JSON.parse(res.data) + // console.log('收到服务器内容:', data); + _this.acceptMessage && _this.acceptMessage(data) + }) + } + // 关闭Socket + closeSocket() { + console.log(9099999) + const _this = this + uni.closeSocket() + _this.socketStart = false + } + // 监听Socket关闭 + monitorSocketClose() { + const _this = this + uni.onSocketClose(function (res) { + console.log('关闭222222222:',res) + + if (res.code = 1000) { + // 放行 STOMP 协议消息,交给 socketTask.onMessage 处理 by 闵 + return; + } + console.log('WebSocket 已关闭!') + _this.socketStart = false + setTimeout(function () { + _this.init(_this.socketType) + }, 3000) + }) + } + // 监听Socket错误 + monitorSocketError() { + const _this = this + uni.onSocketError(function (res) { + _this.socketStart = false + console.log('WebSocket连接打开失败,请检查!') + }) + } + // 心跳 + getHeartbeat() { const userStore = useUserStore() - const _this = this - this.send( - { - type: '心跳', - userUid: userStore.userInfo.userid, - }, - (val) => { - setTimeout(() => { - if (val) { - // _this.getHeartbeat(); - } else { - if (!_this.socketStart) { - // _this.init(); - } - } - }, 10000) - }, - ) - } + const _this = this + this.send( + { + type: '心跳', + userUid: userStore.userInfo.userid, + }, + (val) => { + setTimeout(() => { + if (val) { + // _this.getHeartbeat(); + } else { + if (!_this.socketStart) { + // _this.init(); + } + } + }, 10000) + }, + ) + } } const mySocket = new socket() -export default mySocket +export default mySocket \ No newline at end of file diff --git a/src/pages-home/device-control/office/ALLleader.vue b/src/pages-home/device-control/office/ALLleader.vue index d60bd2f..c7da6a2 100644 --- a/src/pages-home/device-control/office/ALLleader.vue +++ b/src/pages-home/device-control/office/ALLleader.vue @@ -3,7 +3,7 @@ layout: 'default', style: { navigationBarTitleText: '办公室设备列表', - + }, } @@ -39,8 +39,8 @@ -