update 添加websocket功能推送员工报警信息
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { onMounted, onUnmounted, ref } from 'vue';
|
||||
|
||||
const path = 'ws://192.168.1.16:7098/websocket/watchMonitor';
|
||||
const token =
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWRlbnRpZmllciI6ImU5Y2EyM2Q2OGQ4ODRkNGViYjE5ZDA3ODg5NzI3ZGFlIiwiZXhwIjoxNzAyNjA0MDI3fQ.MYf9yUnXO0RTFL4u96pN1Z2mBwtXU5W-QlxDgoAsoOw';
|
||||
|
||||
export function useSocket() {
|
||||
const socket = ref();
|
||||
let socketData = ref({});
|
||||
|
||||
function init() {
|
||||
if (typeof WebSocket === 'undefined') {
|
||||
alert('您的浏览器不支持socket');
|
||||
} else {
|
||||
// 实例化socket
|
||||
socket.value = new WebSocket(path, [token]);
|
||||
// 监听socket连接
|
||||
socket.value.onopen = open;
|
||||
// 监听socket错误信息
|
||||
socket.value.onerror = error;
|
||||
// 监听socket消息
|
||||
socket.value.onmessage = getMessage;
|
||||
}
|
||||
}
|
||||
|
||||
function open() {
|
||||
console.log('socket连接成功');
|
||||
}
|
||||
|
||||
function error() {
|
||||
console.log('socket连接错误');
|
||||
}
|
||||
|
||||
function getMessage(msg) {
|
||||
console.log('y', msg.data);
|
||||
socketData.value = JSON.parse(msg.data);
|
||||
}
|
||||
|
||||
function setSocketData(data) {
|
||||
socketData.value = data;
|
||||
}
|
||||
|
||||
function close() {
|
||||
console.log('socket已经关闭');
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
init();
|
||||
});
|
||||
onUnmounted(() => {
|
||||
socket.value.onclose = close;
|
||||
});
|
||||
return { socket, socketData, setSocketData };
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import * as dayjs from 'dayjs';
|
||||
|
||||
export function getZero(num: any) {
|
||||
if (num < 10) {
|
||||
return '0' + num;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
export function useTopTime() {
|
||||
const timeLeft = dayjs().format('YYYY年MM月DD日 HH时mm分ss秒');
|
||||
const timeRight =
|
||||
dayjs().diff(dayjs('2018-10-01'), 'day') +
|
||||
'天' +
|
||||
getZero(dayjs().diff(dayjs(dayjs().format('YYYY-MM-DD') + ' 00:00:00'), 'hours')) +
|
||||
'时' +
|
||||
getZero(dayjs().diff(dayjs(dayjs().format('YYYY-MM-DD HH') + ':00:00'), 'minutes')) +
|
||||
'分' +
|
||||
getZero(dayjs().diff(dayjs(dayjs().format('YYYY-MM-DD HH:mm') + ':00'), 'seconds')) +
|
||||
'秒';
|
||||
return {
|
||||
timeLeft,
|
||||
timeRight,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user