update
This commit is contained in:
+17
-6
@@ -1,30 +1,40 @@
|
||||
import { onMounted, onUnmounted, ref } from 'vue';
|
||||
import { getAppEnvConfig } from '/@/utils/env.ts';
|
||||
import { useUserStore } from '/@/store/modules/user.ts';
|
||||
import { getAuthCache } from '/@/utils/auth.ts';
|
||||
import { TOKEN_KEY } from '/@/enum/cacheEnum.ts';
|
||||
import { useRouter } from 'vue-router';
|
||||
|
||||
const router = useRouter();
|
||||
const userStore = useUserStore();
|
||||
const { token } = userStore;
|
||||
const { VITE_GLOB_API_YIN_JI, VITE_GLOB_API_JIAN_CE } = getAppEnvConfig();
|
||||
//健康监测工具报警
|
||||
export const basicPath = VITE_GLOB_API_YIN_JI;
|
||||
//应急求助
|
||||
export const emergencyPath = VITE_GLOB_API_JIAN_CE;
|
||||
const token =
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWRlbnRpZmllciI6ImU5Y2EyM2Q2OGQ4ODRkNGViYjE5ZDA3ODg5NzI3ZGFlIiwiZXhwIjoxNzAzMjExNzQ1fQ.dadvKgAgzfK6DUd8xtrgadxPbPXUsayNNSrt3mNxKC8';
|
||||
let t = token || getAuthCache(TOKEN_KEY);
|
||||
|
||||
export function useSocket(path = basicPath) {
|
||||
const socket = ref();
|
||||
let socketData = ref({});
|
||||
|
||||
function init() {
|
||||
if (!t) {
|
||||
return router?.replace('/login');
|
||||
}
|
||||
if (typeof WebSocket === 'undefined') {
|
||||
alert('您的浏览器不支持socket');
|
||||
} else {
|
||||
// 实例化socket
|
||||
socket.value = new WebSocket(path, [token]);
|
||||
socket.value = new WebSocket(path, [t]);
|
||||
// 监听socket连接
|
||||
socket.value.onopen = open;
|
||||
// 监听socket错误信息
|
||||
socket.value.onerror = error;
|
||||
// 监听socket消息
|
||||
socket.value.onmessage = getMessage;
|
||||
socket.value.onclose = close;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +47,7 @@ export function useSocket(path = basicPath) {
|
||||
}
|
||||
|
||||
function getMessage(msg) {
|
||||
console.log('y', msg.data);
|
||||
console.log('getMessage', msg);
|
||||
socketData.value = JSON.parse(msg.data);
|
||||
}
|
||||
|
||||
@@ -45,7 +55,8 @@ export function useSocket(path = basicPath) {
|
||||
socketData.value = data;
|
||||
}
|
||||
|
||||
function close() {
|
||||
function close(data) {
|
||||
console.log(data);
|
||||
console.log('socket已经关闭');
|
||||
}
|
||||
|
||||
@@ -53,7 +64,7 @@ export function useSocket(path = basicPath) {
|
||||
init();
|
||||
});
|
||||
onUnmounted(() => {
|
||||
socket.value.onclose = close;
|
||||
//socket.value.onclose = close;
|
||||
});
|
||||
return { socket, socketData, setSocketData };
|
||||
}
|
||||
Reference in New Issue
Block a user