调整websocket响应
This commit is contained in:
parent
70004f842c
commit
cb258688c7
@ -11,13 +11,16 @@ import { useUserStore } from "@/store/modules/user";
|
||||
const { getUser } = useUserStore();
|
||||
|
||||
const payUrl = ref("");
|
||||
const ws = useWebSocket(`/zhxy/webSocket/${getUser.userId}`, (type: string, data: any) => {
|
||||
console.log('收到WebSocket消息:', type, data);
|
||||
if (type === 'pay') {
|
||||
const ws = useWebSocket(`/zhxy/webSocket/${getUser.userId}`, (type: string, res: any) => {
|
||||
console.log('收到WebSocket消息:', type, res.data);
|
||||
// 将data从字符串转为对象
|
||||
const dataObj = JSON.parse(res.data);
|
||||
if (dataObj.action === 'pay') {
|
||||
uni.showToast({
|
||||
title: '支付成功',
|
||||
icon: 'success',
|
||||
})
|
||||
});
|
||||
ws.closeConnect();
|
||||
// 跳转到支付成功页面
|
||||
setTimeout(() => {
|
||||
uni.reLaunch({
|
||||
@ -38,7 +41,7 @@ onLoad((options: any) => {
|
||||
}
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
onBeforeUnmount(() => {
|
||||
ws.closeConnect();
|
||||
});
|
||||
</script>
|
||||
|
||||
@ -70,6 +70,7 @@ export function useWebSocket(url: string, callback?: Function, OBJECT?: UniNames
|
||||
}
|
||||
|
||||
function closeConnect() {
|
||||
console.log('断开WebSocket连接');
|
||||
// uni-app 提供的关闭方法(如果支持)
|
||||
if (typeof uni.closeSocket === 'function') {
|
||||
uni.closeSocket({
|
||||
@ -82,6 +83,7 @@ export function useWebSocket(url: string, callback?: Function, OBJECT?: UniNames
|
||||
|
||||
//监听WebSocket接受到服务器的消息事件。
|
||||
uni.onSocketMessage(function (res) {
|
||||
console.log('收到服务器内容:' + res.data);
|
||||
if (callback) {
|
||||
callback('data', res)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user