Merge remote-tracking branch 'remotes/origin/master'
This commit is contained in:
commit
8c08c1f3df
4
env/.env.development
vendored
4
env/.env.development
vendored
@ -7,7 +7,9 @@ VITE_SHOW_SOURCEMAP = true
|
|||||||
# 是否启用读取配置文件 min
|
# 是否启用读取配置文件 min
|
||||||
#VITE_WEBAPP = 'D://opt//AppUpdateTest'
|
#VITE_WEBAPP = 'D://opt//AppUpdateTest'
|
||||||
VITE_WEBAPP = ''
|
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://36.112.48.190/jeecg-boot'
|
||||||
#VITE_SERVER_BASEURL = 'https://szcx.zyyt.sinopec.com/jeecg-boot'
|
#VITE_SERVER_BASEURL = 'https://szcx.zyyt.sinopec.com/jeecg-boot'
|
||||||
|
|
||||||
|
@ -1,140 +1,156 @@
|
|||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
|
import { log } from 'console'
|
||||||
import { randomString } from './uitls'
|
import { randomString } from './uitls'
|
||||||
import { useUserStore } from '@/store/user'
|
import { useUserStore } from '@/store/user'
|
||||||
|
|
||||||
const baseUrl = import.meta.env.VITE_SERVER_BASEURL
|
const baseUrl = import.meta.env.VITE_SERVER_BASEURL
|
||||||
|
|
||||||
class socket {
|
class socket {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.socketUrl = baseUrl
|
console.log(77777777777777)
|
||||||
this.socketStart = false
|
this.socketUrl = baseUrl
|
||||||
this.socketType = ''
|
this.socketStart = false
|
||||||
this.monitorSocketError()
|
this.socketType = ''
|
||||||
this.monitorSocketClose()
|
this.monitorSocketError()
|
||||||
this.socketReceive()
|
this.monitorSocketClose()
|
||||||
}
|
this.socketReceive()
|
||||||
init(socket_type, callback?) {
|
}
|
||||||
|
init(socket_type, callback ?) {
|
||||||
const userStore = useUserStore()
|
const userStore = useUserStore()
|
||||||
const _this = this
|
const _this = this
|
||||||
if (baseUrl) {
|
if (baseUrl) {
|
||||||
if (this.socketStart) {
|
if (this.socketStart) {
|
||||||
console.log('webSocket已经启动了')
|
console.log('webSocket已经启动了')
|
||||||
} else {
|
} else {
|
||||||
_this.socketType = socket_type
|
_this.socketType = socket_type
|
||||||
let url =
|
let url =
|
||||||
this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') +
|
this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') +
|
||||||
'/' +
|
'/' +
|
||||||
socket_type +
|
socket_type +
|
||||||
'/' +
|
'/' +
|
||||||
userStore.userInfo.userid +
|
userStore.userInfo.userid +
|
||||||
'_app'
|
'_app'
|
||||||
if (socket_type == 'eoaNewChatSocket') {
|
if (socket_type == 'eoaNewChatSocket') {
|
||||||
let randomMessageId = randomString(6)
|
let randomMessageId = randomString(6)
|
||||||
url =
|
url =
|
||||||
this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') +
|
this.socketUrl.replace('https://', 'wss://').replace('http://', 'ws://') +
|
||||||
'/eoaNewChatSocket/' +
|
'/eoaNewChatSocket/' +
|
||||||
userStore.userInfo.userid +
|
userStore.userInfo.userid +
|
||||||
'/' +
|
'/' +
|
||||||
randomMessageId
|
randomMessageId
|
||||||
}
|
}
|
||||||
console.log('启动this.socketUrl连接地址:', url)
|
console.log('启动this.socketUrl连接地址:', url)
|
||||||
// update-begin-author:taoyan date:20220422 for:v2.4.6 的 websocket 服务端,存在性能和安全问题。 #3278
|
// update-begin-author:taoyan date:20220422 for:v2.4.6 的 websocket 服务端,存在性能和安全问题。 #3278
|
||||||
|
|
||||||
let token = userStore.userInfo.token
|
let token = userStore.userInfo.token
|
||||||
uni.connectSocket({
|
uni.connectSocket({
|
||||||
url: url,
|
url: url,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
protocols: [token],
|
protocols: [token],
|
||||||
})
|
})
|
||||||
// update-end-author:taoyan date:20220422 for: v2.4.6 的 websocket 服务端,存在性能和安全问题。 #3278
|
// update-end-author:taoyan date:20220422 for: v2.4.6 的 websocket 服务端,存在性能和安全问题。 #3278
|
||||||
uni.onSocketOpen((res) => {
|
uni.onSocketOpen((res) => {
|
||||||
this.socketStart = true
|
console.log("连接进来了--------53333333333323----")
|
||||||
callback && callback()
|
this.socketStart = true
|
||||||
console.log('WebSocket连接已打开!')
|
callback && callback()
|
||||||
})
|
console.log('WebSocket连接已打开!')
|
||||||
/* setTimeout(() => {
|
})
|
||||||
_this.getHeartbeat();
|
/* setTimeout(() => {
|
||||||
}, 5000); */
|
_this.getHeartbeat();
|
||||||
}
|
}, 5000); */
|
||||||
} else {
|
}
|
||||||
console.log('config/baseUrl socketUrl为空')
|
} else {
|
||||||
}
|
console.log('config/baseUrl socketUrl为空')
|
||||||
}
|
}
|
||||||
// Socket给服务器发送消息
|
}
|
||||||
send(data, callback) {
|
// Socket给服务器发送消息
|
||||||
|
send(data, callback) {
|
||||||
const userStore = useUserStore()
|
const userStore = useUserStore()
|
||||||
const _this = this
|
const _this = this
|
||||||
if (userStore.userInfo.userid) {
|
if (userStore.userInfo.userid) {
|
||||||
data.userUid = userStore.userInfo.userid
|
data.userUid = userStore.userInfo.userid
|
||||||
}
|
}
|
||||||
console.log(data)
|
console.log(data)
|
||||||
uni.sendSocketMessage({
|
uni.sendSocketMessage({
|
||||||
data: JSON.stringify(data),
|
data: JSON.stringify(data),
|
||||||
success: () => {
|
success: () => {
|
||||||
callback && callback(true)
|
callback && callback(true)
|
||||||
},
|
},
|
||||||
fail: () => {
|
fail: () => {
|
||||||
callback && callback(false)
|
callback && callback(false)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Socket接收服务器发送过来的消息
|
// Socket接收服务器发送过来的消息
|
||||||
socketReceive() {
|
socketReceive() {
|
||||||
const _this = this
|
const _this = this
|
||||||
uni.onSocketMessage(function (res) {
|
uni.onSocketMessage(function (res) {
|
||||||
console.log('APP:--》收到服务器内容:')
|
console.log('APP:--》收到服务器内容:', res)
|
||||||
let data = JSON.parse(res.data)
|
console.log('连接路径----:', baseUrl)
|
||||||
// console.log('收到服务器内容:', data);
|
if (res.data.startsWith('CONNECTED') || res.data.startsWith('MESSAGE') || res.data.startsWith('RECEIPT')) {
|
||||||
_this.acceptMessage && _this.acceptMessage(data)
|
console.log('放行----------:', res)
|
||||||
})
|
// 放行 STOMP 协议消息,交给 socketTask.onMessage 处理 by 闵
|
||||||
}
|
return;
|
||||||
// 关闭Socket
|
}
|
||||||
closeSocket() {
|
let data = JSON.parse(res.data)
|
||||||
const _this = this
|
// console.log('收到服务器内容:', data);
|
||||||
uni.closeSocket()
|
_this.acceptMessage && _this.acceptMessage(data)
|
||||||
_this.socketStart = false
|
})
|
||||||
}
|
}
|
||||||
// 监听Socket关闭
|
// 关闭Socket
|
||||||
monitorSocketClose() {
|
closeSocket() {
|
||||||
const _this = this
|
console.log(9099999)
|
||||||
uni.onSocketClose(function (res) {
|
const _this = this
|
||||||
console.log('WebSocket 已关闭!')
|
uni.closeSocket()
|
||||||
_this.socketStart = false
|
_this.socketStart = false
|
||||||
setTimeout(function () {
|
}
|
||||||
_this.init(_this.socketType)
|
// 监听Socket关闭
|
||||||
}, 3000)
|
monitorSocketClose() {
|
||||||
})
|
const _this = this
|
||||||
}
|
uni.onSocketClose(function (res) {
|
||||||
// 监听Socket错误
|
console.log('关闭222222222:',res)
|
||||||
monitorSocketError() {
|
|
||||||
const _this = this
|
if (res.code = 1000) {
|
||||||
uni.onSocketError(function (res) {
|
// 放行 STOMP 协议消息,交给 socketTask.onMessage 处理 by 闵
|
||||||
_this.socketStart = false
|
return;
|
||||||
console.log('WebSocket连接打开失败,请检查!')
|
}
|
||||||
})
|
console.log('WebSocket 已关闭!')
|
||||||
}
|
_this.socketStart = false
|
||||||
// 心跳
|
setTimeout(function () {
|
||||||
getHeartbeat() {
|
_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 userStore = useUserStore()
|
||||||
const _this = this
|
const _this = this
|
||||||
this.send(
|
this.send(
|
||||||
{
|
{
|
||||||
type: '心跳',
|
type: '心跳',
|
||||||
userUid: userStore.userInfo.userid,
|
userUid: userStore.userInfo.userid,
|
||||||
},
|
},
|
||||||
(val) => {
|
(val) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (val) {
|
if (val) {
|
||||||
// _this.getHeartbeat();
|
// _this.getHeartbeat();
|
||||||
} else {
|
} else {
|
||||||
if (!_this.socketStart) {
|
if (!_this.socketStart) {
|
||||||
// _this.init();
|
// _this.init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 10000)
|
}, 10000)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const mySocket = new socket()
|
const mySocket = new socket()
|
||||||
export default mySocket
|
export default mySocket
|
@ -3,7 +3,7 @@
|
|||||||
layout: 'default',
|
layout: 'default',
|
||||||
style: {
|
style: {
|
||||||
navigationBarTitleText: '办公室设备列表',
|
navigationBarTitleText: '办公室设备列表',
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</route>
|
</route>
|
||||||
@ -39,8 +39,8 @@
|
|||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
<!--
|
<!--
|
||||||
<view class="connection-section">
|
<view class="connection-section">
|
||||||
<button v-if="!connected" @click="connectWebSocket" class="connect-btn">
|
<button v-if="!connected" @click="connectWebSocket" class="connect-btn">
|
||||||
连接WebSocket
|
连接WebSocket
|
||||||
</button>
|
</button>
|
||||||
@ -68,6 +68,13 @@
|
|||||||
import {
|
import {
|
||||||
ref
|
ref
|
||||||
} from 'vue';
|
} from 'vue';
|
||||||
|
import {
|
||||||
|
onLaunch,
|
||||||
|
onShow,
|
||||||
|
onHide,
|
||||||
|
onLoad,
|
||||||
|
onReady
|
||||||
|
} from '@dcloudio/uni-app'
|
||||||
import {
|
import {
|
||||||
gethomelist,
|
gethomelist,
|
||||||
treeRootList,
|
treeRootList,
|
||||||
@ -79,6 +86,7 @@
|
|||||||
listAllRegions,
|
listAllRegions,
|
||||||
Selecthomelist
|
Selecthomelist
|
||||||
} from '@/api/devicecontrol/officedevice';
|
} from '@/api/devicecontrol/officedevice';
|
||||||
|
// import socketforleader from '@/common/socketforleader'
|
||||||
|
|
||||||
// WebSocket状态
|
// WebSocket状态
|
||||||
const connected = ref(false);
|
const connected = ref(false);
|
||||||
@ -100,6 +108,13 @@
|
|||||||
url: url
|
url: url
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 应用初始化
|
||||||
|
onLaunch(() => {
|
||||||
|
console.log('初始化------')
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
/***-------------------------------开始数据处理------------------------**/
|
/***-------------------------------开始数据处理------------------------**/
|
||||||
const getmqttdata = async () => {
|
const getmqttdata = async () => {
|
||||||
await getMqttPushClient().then(res => {
|
await getMqttPushClient().then(res => {
|
||||||
@ -271,17 +286,18 @@
|
|||||||
selectBydata.value = res.result;
|
selectBydata.value = res.result;
|
||||||
console.log("selectBydata.value:", selectBydata.value)
|
console.log("selectBydata.value:", selectBydata.value)
|
||||||
filteredFloors.value = groupByFloor(selectBydata)
|
filteredFloors.value = groupByFloor(selectBydata)
|
||||||
console.log("数据分组-555---", filteredFloors.value[0].rooms)
|
// console.log("数据分组-555---", filteredFloors.value[0].rooms)
|
||||||
//分组完进行房间状态查询,张祥瑞
|
//分组完进行房间状态查询,张祥瑞
|
||||||
getAllswitchByids()
|
getAllswitchByids()
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//----------------树状展示end-----------
|
//----------------树状展示end-----------
|
||||||
|
|
||||||
//------------------------以上为数据处理---end-------------
|
//------------------------以上为数据处理---end-------------
|
||||||
|
|
||||||
//处理返回的消息
|
//处理返回的消息
|
||||||
const handlesocketMessage = (jsonString) => {
|
const handlesocketMessage = (jsonString) => {
|
||||||
|
console.log("1300--666666666666666---", jsonString)
|
||||||
try {
|
try {
|
||||||
const messageObj = JSON.parse(jsonString);
|
const messageObj = JSON.parse(jsonString);
|
||||||
|
|
||||||
@ -306,13 +322,12 @@
|
|||||||
)
|
)
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
//------------------------以下为websocket-----------
|
|
||||||
|
|
||||||
// 修改后的WebSocket地址(使用新的/simple端点)
|
//------------------------以下为websocket-----------
|
||||||
// const WS_URL = 'http://10.75.15.246:8899/mqttprod/ws/simple';
|
const WS_URL = 'wss://szcx.zyyt.sinopec.com/mqttprod/ws/simple';
|
||||||
const WS_URL = 'https://szcx.zyyt.sinopec.com/mqttprod/ws/simple';
|
// const WS_URL = 'ws://10.75.15.246:8899/mqttprod/ws/simple';
|
||||||
// 连接状态文本
|
|
||||||
const connectionStatus = ref('未连接');
|
const connectionStatus = ref('未连接');
|
||||||
|
|
||||||
// STOMP帧构建器
|
// STOMP帧构建器
|
||||||
const buildStompFrame = (command, headers = {}, body = '') => {
|
const buildStompFrame = (command, headers = {}, body = '') => {
|
||||||
let frame = `${command}\n`;
|
let frame = `${command}\n`;
|
||||||
@ -325,40 +340,34 @@
|
|||||||
|
|
||||||
// 连接WebSocket
|
// 连接WebSocket
|
||||||
const connectWebSocket = () => {
|
const connectWebSocket = () => {
|
||||||
|
console.log(123123)
|
||||||
if (connected.value) return;
|
if (connected.value) return;
|
||||||
|
|
||||||
console.log('正在连接WebSocket...');
|
|
||||||
connectionStatus.value = '连接中...';
|
connectionStatus.value = '连接中...';
|
||||||
|
|
||||||
// 先关闭可能存在的旧连接
|
|
||||||
if (socketTask) {
|
if (socketTask) {
|
||||||
socketTask.close({});
|
socketTask.close({});
|
||||||
socketTask = null;
|
socketTask = null;
|
||||||
}
|
}
|
||||||
|
console.log(44444444)
|
||||||
|
|
||||||
// 修复2:使用正确的uni.connectSocket API
|
|
||||||
socketTask = uni.connectSocket({
|
socketTask = uni.connectSocket({
|
||||||
url: WS_URL,
|
url: WS_URL,
|
||||||
protocols: ['v12.stomp', 'v11.stomp'], // 添加STOMP协议支持
|
protocols: ['v12.stomp', 'v11.stomp'],
|
||||||
success: () => {
|
success: () => {
|
||||||
console.log('开始连接WebSocket');
|
console.log('开始连接WebSocket');
|
||||||
},
|
},
|
||||||
fail: (err) => {
|
fail: (err) => {
|
||||||
console.error('连接失败:', err);
|
console.error('连接失败:', err);
|
||||||
connectionStatus.value = '连接失败';
|
connectionStatus.value = '连接失败';
|
||||||
messages.value.push({
|
|
||||||
type: 'error',
|
|
||||||
content: `连接失败: ${err.errMsg || '未知错误'}`,
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
console.log(55555555555)
|
||||||
// 修复3:使用uni.onSocketOpen替代socketTask.onOpen
|
|
||||||
uni.onSocketOpen((res) => {
|
uni.onSocketOpen((res) => {
|
||||||
|
console.log(66666666)
|
||||||
console.log('WebSocket连接已打开', res);
|
console.log('WebSocket连接已打开', res);
|
||||||
connected.value = true;
|
connected.value = true;
|
||||||
connectionStatus.value = '已连接';
|
connectionStatus.value = '连接中...';
|
||||||
|
|
||||||
// 发送STOMP CONNECT帧
|
// 发送STOMP CONNECT帧
|
||||||
const connectFrame = buildStompFrame('CONNECT', {
|
const connectFrame = buildStompFrame('CONNECT', {
|
||||||
@ -367,23 +376,15 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
uni.sendSocketMessage({
|
uni.sendSocketMessage({
|
||||||
data: connectFrame,
|
data: connectFrame
|
||||||
success: () => {
|
|
||||||
messages.value.push({
|
|
||||||
type: 'system',
|
|
||||||
content: '已发送STOMP连接请求',
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
console.log("77777777777")
|
||||||
});
|
});
|
||||||
|
|
||||||
// 监听消息
|
// 监听消息
|
||||||
socketTask.onMessage((res) => {
|
socketTask.onMessage((res) => {
|
||||||
|
console.log("8888888888888888888")
|
||||||
const data = res.data;
|
const data = res.data;
|
||||||
console.log('收到原始消息:', data);
|
|
||||||
|
|
||||||
// STOMP帧解析
|
|
||||||
const [commandLine, ...headerLines] = data.split('\n');
|
const [commandLine, ...headerLines] = data.split('\n');
|
||||||
const emptyLineIndex = headerLines.findIndex(line => line === '');
|
const emptyLineIndex = headerLines.findIndex(line => line === '');
|
||||||
const headers = {};
|
const headers = {};
|
||||||
@ -394,16 +395,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const body = headerLines.slice(emptyLineIndex + 1).join('\n').replace(/\x00$/, '');
|
const body = headerLines.slice(emptyLineIndex + 1).join('\n').replace(/\x00$/, '');
|
||||||
|
console.log("999999999999999", res)
|
||||||
|
console.log("121212121212", commandLine)
|
||||||
switch (commandLine) {
|
switch (commandLine) {
|
||||||
case 'CONNECTED':
|
case 'CONNECTED':
|
||||||
connectionStatus.value = 'STOMP已连接';
|
connectionStatus.value = 'STOMP已连接';
|
||||||
messages.value.push({
|
|
||||||
type: 'system',
|
|
||||||
content: `STOMP协议连接成功,版本: ${headers.version}`,
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
|
|
||||||
// 订阅主题
|
// 订阅主题
|
||||||
const subscribeFrame = buildStompFrame('SUBSCRIBE', {
|
const subscribeFrame = buildStompFrame('SUBSCRIBE', {
|
||||||
id: 'sub-0',
|
id: 'sub-0',
|
||||||
@ -411,56 +407,25 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
socketTask.send({
|
socketTask.send({
|
||||||
data: subscribeFrame,
|
data: subscribeFrame
|
||||||
success: () => {
|
|
||||||
messages.value.push({
|
|
||||||
type: 'system',
|
|
||||||
content: '已订阅消息主题',
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'MESSAGE':
|
case 'MESSAGE':
|
||||||
messages.value.push({
|
|
||||||
type: 'received',
|
|
||||||
content: `收到: ${body}`,
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
handlesocketMessage(body);
|
handlesocketMessage(body);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'ERROR':
|
case 'ERROR':
|
||||||
messages.value.push({
|
console.error('服务器错误:', body);
|
||||||
type: 'error',
|
|
||||||
content: `服务器错误: ${body}`,
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
|
||||||
messages.value.push({
|
|
||||||
type: 'system',
|
|
||||||
content: `未知帧类型: ${commandLine}`,
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 监听错误(保持原有实现)
|
|
||||||
// 监听关闭(保持原有实现)
|
|
||||||
// 监听错误
|
// 监听错误
|
||||||
uni.onSocketError((err) => {
|
uni.onSocketError((err) => {
|
||||||
console.error('WebSocket错误:', err);
|
console.error('WebSocket错误:', err);
|
||||||
connectionStatus.value = '连接错误';
|
connectionStatus.value = '连接错误';
|
||||||
connected.value = false;
|
connected.value = false;
|
||||||
|
|
||||||
messages.value.push({
|
|
||||||
type: 'error',
|
|
||||||
content: `连接错误: ${err.errMsg || '未知错误'}`,
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 监听关闭
|
// 监听关闭
|
||||||
@ -468,12 +433,6 @@
|
|||||||
console.log('WebSocket连接已关闭', closeRes);
|
console.log('WebSocket连接已关闭', closeRes);
|
||||||
connectionStatus.value = '已断开';
|
connectionStatus.value = '已断开';
|
||||||
connected.value = false;
|
connected.value = false;
|
||||||
|
|
||||||
messages.value.push({
|
|
||||||
type: 'system',
|
|
||||||
content: `WebSocket连接已关闭: ${closeRes.code || ''} ${closeRes.reason || ''}`,
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -496,31 +455,19 @@
|
|||||||
socketTask.send({
|
socketTask.send({
|
||||||
data: sendFrame,
|
data: sendFrame,
|
||||||
success: () => {
|
success: () => {
|
||||||
messages.value.push({
|
|
||||||
type: 'sent',
|
|
||||||
content: `发送: ${message.value}`,
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
message.value = '';
|
message.value = '';
|
||||||
},
|
|
||||||
fail: (err) => {
|
|
||||||
messages.value.push({
|
|
||||||
type: 'error',
|
|
||||||
content: `发送失败: ${err.errMsg}`,
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// 断开WebSocket(优化实现)
|
// 断开WebSocket
|
||||||
const disconnectWebSocket = () => {
|
const disconnectWebSocket = () => {
|
||||||
if (socketTask) {
|
if (socketTask) {
|
||||||
// 发送STOMP DISCONNECT帧
|
// 发送STOMP DISCONNECT帧
|
||||||
const disconnectFrame = buildStompFrame('DISCONNECT', {
|
const disconnectFrame = buildStompFrame('DISCONNECT', {
|
||||||
receipt: 'disconnect-receipt'
|
receipt: 'disconnect-receipt'
|
||||||
});
|
});
|
||||||
|
console.log(socketTask)
|
||||||
socketTask.send({
|
socketTask.send({
|
||||||
data: disconnectFrame,
|
data: disconnectFrame,
|
||||||
success: () => {
|
success: () => {
|
||||||
@ -528,14 +475,8 @@
|
|||||||
code: 1000,
|
code: 1000,
|
||||||
reason: '用户手动关闭',
|
reason: '用户手动关闭',
|
||||||
success: () => {
|
success: () => {
|
||||||
console.log('WebSocket已关闭');
|
|
||||||
connected.value = false;
|
connected.value = false;
|
||||||
connectionStatus.value = '已断开';
|
connectionStatus.value = '已断开';
|
||||||
messages.value.push({
|
|
||||||
type: 'system',
|
|
||||||
content: '连接已安全关闭',
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -544,6 +485,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
|
// disconnectWebSocket();
|
||||||
loadtree();
|
loadtree();
|
||||||
connectWebSocket();
|
connectWebSocket();
|
||||||
getmqttdata();
|
getmqttdata();
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
<!-- <view class="connection-section">
|
<!-- <view class="connection-section">
|
||||||
<button v-if="!connected" @click="connectWebSocket" class="connect-btn">
|
<button v-if="!connected" @click="connectWebSocket" class="connect-btn">
|
||||||
连接WebSocket
|
连接WebSocket
|
||||||
</button>
|
</button>
|
||||||
@ -54,10 +54,10 @@
|
|||||||
listAllRegions,
|
listAllRegions,
|
||||||
Selecthomelist
|
Selecthomelist
|
||||||
} from '@/api/devicecontrol/officedevice';
|
} from '@/api/devicecontrol/officedevice';
|
||||||
import useWebSocketService from '@/api/devicecontrol/webSocketService'
|
// import useWebSocketService from '@/api/devicecontrol/webSocketService'
|
||||||
|
|
||||||
// 0717张祥瑞
|
// 0717张祥瑞
|
||||||
const wsService = useWebSocketService()
|
// const wsService = useWebSocketService()
|
||||||
const unsubscribe = ref(null)
|
const unsubscribe = ref(null)
|
||||||
// WebSocket状态
|
// WebSocket状态
|
||||||
const connected = ref(false);
|
const connected = ref(false);
|
||||||
@ -253,7 +253,7 @@
|
|||||||
|
|
||||||
//处理返回的消息
|
//处理返回的消息
|
||||||
const handlesocketMessage = (jsonString) => {
|
const handlesocketMessage = (jsonString) => {
|
||||||
console.log("1300-----",jsonString)
|
console.log("1300-----", jsonString)
|
||||||
try {
|
try {
|
||||||
const messageObj = JSON.parse(jsonString);
|
const messageObj = JSON.parse(jsonString);
|
||||||
|
|
||||||
@ -280,8 +280,8 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
//------------------------以下为websocket-----------
|
//------------------------以下为websocket-----------
|
||||||
// const WS_URL = 'https://szcx.zyyt.sinopec.com/mqttprod/ws/simple';
|
const WS_URL = 'wss://szcx.zyyt.sinopec.com/mqttprod/ws/simple';
|
||||||
// const WS_URL = 'http://10.75.15.246:8899/mqttprod/ws/simple';
|
// const WS_URL = 'ws://10.75.15.246:8899/mqttprod/ws/simple';
|
||||||
const connectionStatus = ref('未连接');
|
const connectionStatus = ref('未连接');
|
||||||
|
|
||||||
// STOMP帧构建器
|
// STOMP帧构建器
|
||||||
@ -296,25 +296,146 @@
|
|||||||
|
|
||||||
// 连接WebSocket
|
// 连接WebSocket
|
||||||
const connectWebSocket = () => {
|
const connectWebSocket = () => {
|
||||||
const WS_URL = 'https://szcx.zyyt.sinopec.com/mqttprod/ws/simple'
|
console.log(123123)
|
||||||
// const WS_URL = 'http://10.75.15.246:8899/mqttprod/ws/simple';
|
if (connected.value) return;
|
||||||
wsService.connectWebSocket(WS_URL, '/topic/messages')
|
|
||||||
}
|
connectionStatus.value = '连接中...';
|
||||||
|
|
||||||
|
if (socketTask) {
|
||||||
|
socketTask.close({});
|
||||||
|
socketTask = null;
|
||||||
|
}
|
||||||
|
console.log(44444444)
|
||||||
|
|
||||||
|
socketTask = uni.connectSocket({
|
||||||
|
url: WS_URL,
|
||||||
|
protocols: ['v12.stomp', 'v11.stomp'],
|
||||||
|
success: () => {
|
||||||
|
console.log('开始连接WebSocket');
|
||||||
|
},
|
||||||
|
fail: (err) => {
|
||||||
|
console.error('连接失败:', err);
|
||||||
|
connectionStatus.value = '连接失败';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
console.log(55555555555)
|
||||||
|
uni.onSocketOpen((res) => {
|
||||||
|
console.log(66666666)
|
||||||
|
console.log('WebSocket连接已打开', res);
|
||||||
|
connected.value = true;
|
||||||
|
connectionStatus.value = '连接中...';
|
||||||
|
|
||||||
|
// 发送STOMP CONNECT帧
|
||||||
|
const connectFrame = buildStompFrame('CONNECT', {
|
||||||
|
'accept-version': '1.2',
|
||||||
|
'heart-beat': '10000,10000'
|
||||||
|
});
|
||||||
|
|
||||||
|
uni.sendSocketMessage({
|
||||||
|
data: connectFrame
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 监听消息
|
||||||
|
socketTask.onMessage((res) => {
|
||||||
|
const data = res.data;
|
||||||
|
const [commandLine, ...headerLines] = data.split('\n');
|
||||||
|
const emptyLineIndex = headerLines.findIndex(line => line === '');
|
||||||
|
const headers = {};
|
||||||
|
|
||||||
|
for (let i = 0; i < emptyLineIndex; i++) {
|
||||||
|
const [key, value] = headerLines[i].split(':');
|
||||||
|
headers[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = headerLines.slice(emptyLineIndex + 1).join('\n').replace(/\x00$/, '');
|
||||||
|
|
||||||
|
switch (commandLine) {
|
||||||
|
case 'CONNECTED':
|
||||||
|
connectionStatus.value = 'STOMP已连接';
|
||||||
|
// 订阅主题
|
||||||
|
const subscribeFrame = buildStompFrame('SUBSCRIBE', {
|
||||||
|
id: 'sub-0',
|
||||||
|
destination: '/topic/messages'
|
||||||
|
});
|
||||||
|
|
||||||
|
socketTask.send({
|
||||||
|
data: subscribeFrame
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'MESSAGE':
|
||||||
|
handlesocketMessage(body);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'ERROR':
|
||||||
|
console.error('服务器错误:', body);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 监听错误
|
||||||
|
uni.onSocketError((err) => {
|
||||||
|
console.error('WebSocket错误:', err);
|
||||||
|
connectionStatus.value = '连接错误';
|
||||||
|
connected.value = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 监听关闭
|
||||||
|
uni.onSocketClose((closeRes) => {
|
||||||
|
console.log('WebSocket连接已关闭', closeRes);
|
||||||
|
connectionStatus.value = '已断开';
|
||||||
|
connected.value = false;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// 发送消息
|
||||||
|
const sendMessage = () => {
|
||||||
|
if (!message.value.trim()) {
|
||||||
|
uni.showToast({
|
||||||
|
title: '消息不能为空',
|
||||||
|
icon: 'none'
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建STOMP SEND帧
|
||||||
|
const sendFrame = buildStompFrame('SEND', {
|
||||||
|
destination: '/app/chat',
|
||||||
|
'content-type': 'text/plain'
|
||||||
|
}, message.value);
|
||||||
|
|
||||||
|
socketTask.send({
|
||||||
|
data: sendFrame,
|
||||||
|
success: () => {
|
||||||
|
message.value = '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// 断开WebSocket
|
// 断开WebSocket
|
||||||
const disconnectWebSocket = () => {
|
const disconnectWebSocket = () => {
|
||||||
wsService.disconnectWebSocket()
|
if (socketTask) {
|
||||||
|
// 发送STOMP DISCONNECT帧
|
||||||
|
const disconnectFrame = buildStompFrame('DISCONNECT', {
|
||||||
|
receipt: 'disconnect-receipt'
|
||||||
|
});
|
||||||
|
console.log(socketTask)
|
||||||
|
socketTask.send({
|
||||||
|
data: disconnectFrame,
|
||||||
|
success: () => {
|
||||||
|
socketTask.close({
|
||||||
|
code: 1000,
|
||||||
|
reason: '用户手动关闭',
|
||||||
|
success: () => {
|
||||||
|
connected.value = false;
|
||||||
|
connectionStatus.value = '已断开';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// 订阅消息
|
|
||||||
unsubscribe.value = wsService.subscribe((message) => {
|
|
||||||
handlesocketMessage(message)
|
|
||||||
messages.value.push({
|
|
||||||
content: message,
|
|
||||||
type: 'received',
|
|
||||||
time: new Date().toLocaleTimeString()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
loadtree();
|
loadtree();
|
||||||
@ -322,12 +443,20 @@
|
|||||||
getmqttdata();
|
getmqttdata();
|
||||||
getofficehomelist();
|
getofficehomelist();
|
||||||
})
|
})
|
||||||
onUnmounted(() => {
|
|
||||||
// 取消订阅
|
onUnload(() => {
|
||||||
if (unsubscribe.value) {
|
disconnectWebSocket();
|
||||||
unsubscribe.value()
|
// // 监听关闭
|
||||||
}
|
// uni.onSocketClose((closeRes) => {
|
||||||
// 不需要在这里断开连接,因为其他页面可能还在使用
|
// console.log('WebSocket连接已关闭', closeRes);
|
||||||
|
// connectionStatus.value = '已断开';
|
||||||
|
// connected.value = false;
|
||||||
|
// // messages.value.push({
|
||||||
|
// // type: 'system',
|
||||||
|
// // content: `WebSocket连接已关闭: ${closeRes.code || ''} ${closeRes.reason || ''}`,
|
||||||
|
// // timestamp: Date.now()
|
||||||
|
// // });
|
||||||
|
// });
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -64,8 +64,8 @@
|
|||||||
//1455737215009562625 厂书记,
|
//1455737215009562625 厂书记,
|
||||||
//1455737513371377665 厂领导,
|
//1455737513371377665 厂领导,
|
||||||
//1358226526012129281 厂主管领导
|
//1358226526012129281 厂主管领导
|
||||||
console.log("当前登陆人信息000", userStore.userInfo.roles)
|
// console.log("当前登陆人信息000", userStore.userInfo.roles)
|
||||||
console.log("当前登陆人信息222222222222", userStore.userInfo.roles.split(","))
|
// console.log("当前登陆人信息222222222222", userStore.userInfo.roles.split(","))
|
||||||
|
|
||||||
for (let a = 0; a < userStore.userInfo.roles.split(",").length; a++) {
|
for (let a = 0; a < userStore.userInfo.roles.split(",").length; a++) {
|
||||||
console.log("userStore.userInfo.roles.split(", ")[a]:", userStore.userInfo.roles.split(",")[a])
|
console.log("userStore.userInfo.roles.split(", ")[a]:", userStore.userInfo.roles.split(",")[a])
|
||||||
@ -76,7 +76,7 @@
|
|||||||
"1455737513371377665" || rolesid ==
|
"1455737513371377665" || rolesid ==
|
||||||
"1358226526012129281") { //判断人员
|
"1358226526012129281") { //判断人员
|
||||||
isleader.value = true;
|
isleader.value = true;
|
||||||
console.log("isleader-------", isleader.value)
|
// console.log("isleader-------", isleader.value)
|
||||||
} else {
|
} else {
|
||||||
isleader.value = false;
|
isleader.value = false;
|
||||||
}
|
}
|
||||||
|
@ -94,6 +94,7 @@ const init = () => {
|
|||||||
navTitle.value = params.fromUserName
|
navTitle.value = params.fromUserName
|
||||||
chatto.value = chatObj.value.msgTo
|
chatto.value = chatObj.value.msgTo
|
||||||
creatFriendSession(chatObj.value.msgTo)
|
creatFriendSession(chatObj.value.msgTo)
|
||||||
|
console.log(7777777777777777777777777777)
|
||||||
onSocketOpen()
|
onSocketOpen()
|
||||||
onSocketReceive()
|
onSocketReceive()
|
||||||
getMsgList()
|
getMsgList()
|
||||||
|
Loading…
Reference in New Issue
Block a user