Merge branch 'zxrapp' into 'master'
7.24张祥瑞 See merge request cxcxt/jeecguniapp!29
This commit is contained in:
commit
4543e9d5d1
4
env/.env.development
vendored
4
env/.env.development
vendored
@ -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'
|
||||
|
||||
|
@ -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
|
@ -39,8 +39,8 @@
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<!--
|
||||
<view class="connection-section">
|
||||
<!--
|
||||
<view class="connection-section">
|
||||
<button v-if="!connected" @click="connectWebSocket" class="connect-btn">
|
||||
连接WebSocket
|
||||
</button>
|
||||
@ -68,6 +68,13 @@
|
||||
import {
|
||||
ref
|
||||
} from 'vue';
|
||||
import {
|
||||
onLaunch,
|
||||
onShow,
|
||||
onHide,
|
||||
onLoad,
|
||||
onReady
|
||||
} from '@dcloudio/uni-app'
|
||||
import {
|
||||
gethomelist,
|
||||
treeRootList,
|
||||
@ -79,6 +86,7 @@
|
||||
listAllRegions,
|
||||
Selecthomelist
|
||||
} from '@/api/devicecontrol/officedevice';
|
||||
// import socketforleader from '@/common/socketforleader'
|
||||
|
||||
// WebSocket状态
|
||||
const connected = ref(false);
|
||||
@ -100,6 +108,13 @@
|
||||
url: url
|
||||
});
|
||||
}
|
||||
|
||||
// 应用初始化
|
||||
onLaunch(() => {
|
||||
console.log('初始化------')
|
||||
})
|
||||
|
||||
|
||||
/***-------------------------------开始数据处理------------------------**/
|
||||
const getmqttdata = async () => {
|
||||
await getMqttPushClient().then(res => {
|
||||
@ -271,17 +286,18 @@
|
||||
selectBydata.value = res.result;
|
||||
console.log("selectBydata.value:", selectBydata.value)
|
||||
filteredFloors.value = groupByFloor(selectBydata)
|
||||
console.log("数据分组-555---", filteredFloors.value[0].rooms)
|
||||
// console.log("数据分组-555---", filteredFloors.value[0].rooms)
|
||||
//分组完进行房间状态查询,张祥瑞
|
||||
getAllswitchByids()
|
||||
|
||||
}
|
||||
}
|
||||
//----------------树状展示end-----------
|
||||
|
||||
//------------------------以上为数据处理---end-------------
|
||||
|
||||
//处理返回的消息
|
||||
const handlesocketMessage = (jsonString) => {
|
||||
console.log("1300--666666666666666---", jsonString)
|
||||
try {
|
||||
const messageObj = JSON.parse(jsonString);
|
||||
|
||||
@ -306,13 +322,12 @@
|
||||
)
|
||||
}));
|
||||
};
|
||||
//------------------------以下为websocket-----------
|
||||
|
||||
// 修改后的WebSocket地址(使用新的/simple端点)
|
||||
// const WS_URL = 'http://10.75.15.246:8899/mqttprod/ws/simple';
|
||||
const WS_URL = 'https://szcx.zyyt.sinopec.com/mqttprod/ws/simple';
|
||||
// 连接状态文本
|
||||
//------------------------以下为websocket-----------
|
||||
const WS_URL = 'wss://szcx.zyyt.sinopec.com/mqttprod/ws/simple';
|
||||
// const WS_URL = 'ws://10.75.15.246:8899/mqttprod/ws/simple';
|
||||
const connectionStatus = ref('未连接');
|
||||
|
||||
// STOMP帧构建器
|
||||
const buildStompFrame = (command, headers = {}, body = '') => {
|
||||
let frame = `${command}\n`;
|
||||
@ -325,40 +340,34 @@
|
||||
|
||||
// 连接WebSocket
|
||||
const connectWebSocket = () => {
|
||||
console.log(123123)
|
||||
if (connected.value) return;
|
||||
|
||||
console.log('正在连接WebSocket...');
|
||||
connectionStatus.value = '连接中...';
|
||||
|
||||
// 先关闭可能存在的旧连接
|
||||
if (socketTask) {
|
||||
socketTask.close({});
|
||||
socketTask = null;
|
||||
}
|
||||
console.log(44444444)
|
||||
|
||||
// 修复2:使用正确的uni.connectSocket API
|
||||
socketTask = uni.connectSocket({
|
||||
url: WS_URL,
|
||||
protocols: ['v12.stomp', 'v11.stomp'], // 添加STOMP协议支持
|
||||
protocols: ['v12.stomp', 'v11.stomp'],
|
||||
success: () => {
|
||||
console.log('开始连接WebSocket');
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('连接失败:', err);
|
||||
connectionStatus.value = '连接失败';
|
||||
messages.value.push({
|
||||
type: 'error',
|
||||
content: `连接失败: ${err.errMsg || '未知错误'}`,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 修复3:使用uni.onSocketOpen替代socketTask.onOpen
|
||||
console.log(55555555555)
|
||||
uni.onSocketOpen((res) => {
|
||||
console.log(66666666)
|
||||
console.log('WebSocket连接已打开', res);
|
||||
connected.value = true;
|
||||
connectionStatus.value = '已连接';
|
||||
connectionStatus.value = '连接中...';
|
||||
|
||||
// 发送STOMP CONNECT帧
|
||||
const connectFrame = buildStompFrame('CONNECT', {
|
||||
@ -367,23 +376,15 @@
|
||||
});
|
||||
|
||||
uni.sendSocketMessage({
|
||||
data: connectFrame,
|
||||
success: () => {
|
||||
messages.value.push({
|
||||
type: 'system',
|
||||
content: '已发送STOMP连接请求',
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
data: connectFrame
|
||||
});
|
||||
console.log("77777777777")
|
||||
});
|
||||
|
||||
// 监听消息
|
||||
socketTask.onMessage((res) => {
|
||||
console.log("8888888888888888888")
|
||||
const data = res.data;
|
||||
console.log('收到原始消息:', data);
|
||||
|
||||
// STOMP帧解析
|
||||
const [commandLine, ...headerLines] = data.split('\n');
|
||||
const emptyLineIndex = headerLines.findIndex(line => line === '');
|
||||
const headers = {};
|
||||
@ -394,16 +395,11 @@
|
||||
}
|
||||
|
||||
const body = headerLines.slice(emptyLineIndex + 1).join('\n').replace(/\x00$/, '');
|
||||
|
||||
console.log("999999999999999", res)
|
||||
console.log("121212121212", commandLine)
|
||||
switch (commandLine) {
|
||||
case 'CONNECTED':
|
||||
connectionStatus.value = 'STOMP已连接';
|
||||
messages.value.push({
|
||||
type: 'system',
|
||||
content: `STOMP协议连接成功,版本: ${headers.version}`,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
// 订阅主题
|
||||
const subscribeFrame = buildStompFrame('SUBSCRIBE', {
|
||||
id: 'sub-0',
|
||||
@ -411,56 +407,25 @@
|
||||
});
|
||||
|
||||
socketTask.send({
|
||||
data: subscribeFrame,
|
||||
success: () => {
|
||||
messages.value.push({
|
||||
type: 'system',
|
||||
content: '已订阅消息主题',
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
data: subscribeFrame
|
||||
});
|
||||
break;
|
||||
|
||||
case 'MESSAGE':
|
||||
messages.value.push({
|
||||
type: 'received',
|
||||
content: `收到: ${body}`,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
handlesocketMessage(body);
|
||||
break;
|
||||
|
||||
case 'ERROR':
|
||||
messages.value.push({
|
||||
type: 'error',
|
||||
content: `服务器错误: ${body}`,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
console.error('服务器错误:', body);
|
||||
break;
|
||||
|
||||
default:
|
||||
messages.value.push({
|
||||
type: 'system',
|
||||
content: `未知帧类型: ${commandLine}`,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// 监听错误(保持原有实现)
|
||||
// 监听关闭(保持原有实现)
|
||||
// 监听错误
|
||||
uni.onSocketError((err) => {
|
||||
console.error('WebSocket错误:', err);
|
||||
connectionStatus.value = '连接错误';
|
||||
connected.value = false;
|
||||
|
||||
messages.value.push({
|
||||
type: 'error',
|
||||
content: `连接错误: ${err.errMsg || '未知错误'}`,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
});
|
||||
|
||||
// 监听关闭
|
||||
@ -468,12 +433,6 @@
|
||||
console.log('WebSocket连接已关闭', closeRes);
|
||||
connectionStatus.value = '已断开';
|
||||
connected.value = false;
|
||||
|
||||
messages.value.push({
|
||||
type: 'system',
|
||||
content: `WebSocket连接已关闭: ${closeRes.code || ''} ${closeRes.reason || ''}`,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@ -496,31 +455,19 @@
|
||||
socketTask.send({
|
||||
data: sendFrame,
|
||||
success: () => {
|
||||
messages.value.push({
|
||||
type: 'sent',
|
||||
content: `发送: ${message.value}`,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
message.value = '';
|
||||
},
|
||||
fail: (err) => {
|
||||
messages.value.push({
|
||||
type: 'error',
|
||||
content: `发送失败: ${err.errMsg}`,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 断开WebSocket(优化实现)
|
||||
// 断开WebSocket
|
||||
const disconnectWebSocket = () => {
|
||||
if (socketTask) {
|
||||
// 发送STOMP DISCONNECT帧
|
||||
const disconnectFrame = buildStompFrame('DISCONNECT', {
|
||||
receipt: 'disconnect-receipt'
|
||||
});
|
||||
|
||||
console.log(socketTask)
|
||||
socketTask.send({
|
||||
data: disconnectFrame,
|
||||
success: () => {
|
||||
@ -528,14 +475,8 @@
|
||||
code: 1000,
|
||||
reason: '用户手动关闭',
|
||||
success: () => {
|
||||
console.log('WebSocket已关闭');
|
||||
connected.value = false;
|
||||
connectionStatus.value = '已断开';
|
||||
messages.value.push({
|
||||
type: 'system',
|
||||
content: '连接已安全关闭',
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -544,6 +485,7 @@
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
// disconnectWebSocket();
|
||||
loadtree();
|
||||
connectWebSocket();
|
||||
getmqttdata();
|
||||
|
@ -16,7 +16,7 @@
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<!-- <view class="connection-section">
|
||||
<!-- <view class="connection-section">
|
||||
<button v-if="!connected" @click="connectWebSocket" class="connect-btn">
|
||||
连接WebSocket
|
||||
</button>
|
||||
@ -54,10 +54,10 @@
|
||||
listAllRegions,
|
||||
Selecthomelist
|
||||
} from '@/api/devicecontrol/officedevice';
|
||||
import useWebSocketService from '@/api/devicecontrol/webSocketService'
|
||||
// import useWebSocketService from '@/api/devicecontrol/webSocketService'
|
||||
|
||||
// 0717张祥瑞
|
||||
const wsService = useWebSocketService()
|
||||
// const wsService = useWebSocketService()
|
||||
const unsubscribe = ref(null)
|
||||
// WebSocket状态
|
||||
const connected = ref(false);
|
||||
@ -253,7 +253,7 @@
|
||||
|
||||
//处理返回的消息
|
||||
const handlesocketMessage = (jsonString) => {
|
||||
console.log("1300-----",jsonString)
|
||||
console.log("1300-----", jsonString)
|
||||
try {
|
||||
const messageObj = JSON.parse(jsonString);
|
||||
|
||||
@ -280,8 +280,8 @@
|
||||
};
|
||||
|
||||
//------------------------以下为websocket-----------
|
||||
// const WS_URL = 'https://szcx.zyyt.sinopec.com/mqttprod/ws/simple';
|
||||
// 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 = 'ws://10.75.15.246:8899/mqttprod/ws/simple';
|
||||
const connectionStatus = ref('未连接');
|
||||
|
||||
// STOMP帧构建器
|
||||
@ -296,25 +296,146 @@
|
||||
|
||||
// 连接WebSocket
|
||||
const connectWebSocket = () => {
|
||||
const WS_URL = 'https://szcx.zyyt.sinopec.com/mqttprod/ws/simple'
|
||||
// const WS_URL = 'http://10.75.15.246:8899/mqttprod/ws/simple';
|
||||
wsService.connectWebSocket(WS_URL, '/topic/messages')
|
||||
}
|
||||
console.log(123123)
|
||||
if (connected.value) return;
|
||||
|
||||
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
|
||||
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(() => {
|
||||
loadtree();
|
||||
@ -322,12 +443,20 @@
|
||||
getmqttdata();
|
||||
getofficehomelist();
|
||||
})
|
||||
onUnmounted(() => {
|
||||
// 取消订阅
|
||||
if (unsubscribe.value) {
|
||||
unsubscribe.value()
|
||||
}
|
||||
// 不需要在这里断开连接,因为其他页面可能还在使用
|
||||
|
||||
onUnload(() => {
|
||||
disconnectWebSocket();
|
||||
// // 监听关闭
|
||||
// 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>
|
||||
|
||||
|
@ -64,8 +64,8 @@
|
||||
//1455737215009562625 厂书记,
|
||||
//1455737513371377665 厂领导,
|
||||
//1358226526012129281 厂主管领导
|
||||
console.log("当前登陆人信息000", userStore.userInfo.roles)
|
||||
console.log("当前登陆人信息222222222222", userStore.userInfo.roles.split(","))
|
||||
// console.log("当前登陆人信息000", userStore.userInfo.roles)
|
||||
// console.log("当前登陆人信息222222222222", userStore.userInfo.roles.split(","))
|
||||
|
||||
for (let a = 0; a < userStore.userInfo.roles.split(",").length; a++) {
|
||||
console.log("userStore.userInfo.roles.split(", ")[a]:", userStore.userInfo.roles.split(",")[a])
|
||||
@ -76,7 +76,7 @@
|
||||
"1455737513371377665" || rolesid ==
|
||||
"1358226526012129281") { //判断人员
|
||||
isleader.value = true;
|
||||
console.log("isleader-------", isleader.value)
|
||||
// console.log("isleader-------", isleader.value)
|
||||
} else {
|
||||
isleader.value = false;
|
||||
}
|
||||
|
@ -94,6 +94,7 @@ const init = () => {
|
||||
navTitle.value = params.fromUserName
|
||||
chatto.value = chatObj.value.msgTo
|
||||
creatFriendSession(chatObj.value.msgTo)
|
||||
console.log(7777777777777777777777777777)
|
||||
onSocketOpen()
|
||||
onSocketReceive()
|
||||
getMsgList()
|
||||
|
Loading…
Reference in New Issue
Block a user