commit
1606dbd76f
@ -1,63 +0,0 @@
|
||||
import request from '@/utils/request';
|
||||
import { AxiosPromise } from 'axios';
|
||||
import { NodeConfigVO, NodeConfigForm, NodeConfigQuery } from '@/api/workflow/nodeConfig/types';
|
||||
|
||||
/**
|
||||
* 查询节点配置列表
|
||||
* @param query
|
||||
* @returns {*}
|
||||
*/
|
||||
|
||||
export const listNodeConfig = (query?: NodeConfigQuery): AxiosPromise<NodeConfigVO[]> => {
|
||||
return request({
|
||||
url: '/workflow/nodeConfig/list',
|
||||
method: 'get',
|
||||
params: query
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 查询节点配置详细
|
||||
* @param id
|
||||
*/
|
||||
export const getNodeConfig = (id: string | number): AxiosPromise<NodeConfigVO> => {
|
||||
return request({
|
||||
url: '/workflow/nodeConfig/' + id,
|
||||
method: 'get'
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 新增节点配置
|
||||
* @param data
|
||||
*/
|
||||
export const addNodeConfig = (data: NodeConfigForm) => {
|
||||
return request({
|
||||
url: '/workflow/nodeConfig',
|
||||
method: 'post',
|
||||
data: data
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 修改节点配置
|
||||
* @param data
|
||||
*/
|
||||
export const updateNodeConfig = (data: NodeConfigForm) => {
|
||||
return request({
|
||||
url: '/workflow/nodeConfig',
|
||||
method: 'put',
|
||||
data: data
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 删除节点配置
|
||||
* @param id
|
||||
*/
|
||||
export const delNodeConfig = (id: string | number | Array<string | number>) => {
|
||||
return request({
|
||||
url: '/workflow/nodeConfig/' + id,
|
||||
method: 'delete'
|
||||
});
|
||||
};
|
||||
@ -0,0 +1,45 @@
|
||||
import { getToken } from '@/utils/auth';
|
||||
import { ElNotification } from 'element-plus';
|
||||
import useNoticeStore from '@/store/modules/notice';
|
||||
|
||||
let message = '';
|
||||
|
||||
// 初始化
|
||||
export const initSSE = (url: any) => {
|
||||
url = url + '?Authorization=Bearer ' + getToken() + '&clientid=' + import.meta.env.VITE_APP_CLIENT_ID
|
||||
const {
|
||||
data,
|
||||
error
|
||||
} = useEventSource(url, [], {
|
||||
autoReconnect: {
|
||||
retries: 10,
|
||||
delay: 3000,
|
||||
onFailed() {
|
||||
console.log('Failed to connect after 10 retries')
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
watch(error, () => {
|
||||
console.log('SSE connection error:', error.value)
|
||||
error.value = null;
|
||||
});
|
||||
|
||||
watch(data, () => {
|
||||
if (!data.value) return;
|
||||
useNoticeStore().addNotice({
|
||||
message: data.value,
|
||||
read: false,
|
||||
time: new Date().toLocaleString()
|
||||
});
|
||||
ElNotification({
|
||||
title: '消息',
|
||||
message: data.value,
|
||||
type: 'success',
|
||||
duration: 3000
|
||||
});
|
||||
data.value = null;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@ -1,139 +1,51 @@
|
||||
/**
|
||||
* @module initWebSocket 初始化
|
||||
* @module websocketonopen 连接成功
|
||||
* @module websocketonerror 连接失败
|
||||
* @module websocketclose 断开连接
|
||||
* @module resetHeart 重置心跳
|
||||
* @module sendSocketHeart 心跳发送
|
||||
* @module reconnect 重连
|
||||
* @module sendMsg 发送数据
|
||||
* @module websocketonmessage 接收数据
|
||||
* @module test 测试收到消息传递
|
||||
* @description socket 通信
|
||||
* @param {any} url socket地址
|
||||
* @param {any} websocket websocket 实例
|
||||
* @param {any} heartTime 心跳定时器实例
|
||||
* @param {number} socketHeart 心跳次数
|
||||
* @param {number} HeartTimeOut 心跳超时时间
|
||||
* @param {number} socketError 错误次数
|
||||
*/
|
||||
|
||||
import { getToken } from '@/utils/auth';
|
||||
import { ElNotification } from 'element-plus';
|
||||
import useNoticeStore from '@/store/modules/notice';
|
||||
|
||||
let socketUrl: any = ''; // socket地址
|
||||
let websocket: any = null; // websocket 实例
|
||||
let heartTime: any = null; // 心跳定时器实例
|
||||
let socketHeart = 0 as number; // 心跳次数
|
||||
const HeartTimeOut = 10000; // 心跳超时时间 10000 = 10s
|
||||
let socketError = 0 as number; // 错误次数
|
||||
|
||||
// 初始化socket
|
||||
export const initWebSocket = (url: any) => {
|
||||
if (import.meta.env.VITE_APP_WEBSOCKET === 'false') {
|
||||
return;
|
||||
}
|
||||
socketUrl = url;
|
||||
// 初始化 websocket
|
||||
websocket = new WebSocket(url + '?Authorization=Bearer ' + getToken() + '&clientid=' + import.meta.env.VITE_APP_CLIENT_ID);
|
||||
websocketonopen();
|
||||
websocketonmessage();
|
||||
websocketonerror();
|
||||
websocketclose();
|
||||
sendSocketHeart();
|
||||
return websocket;
|
||||
};
|
||||
|
||||
// socket 连接成功
|
||||
export const websocketonopen = () => {
|
||||
websocket.onopen = function () {
|
||||
console.log('连接 websocket 成功');
|
||||
resetHeart();
|
||||
};
|
||||
};
|
||||
|
||||
// socket 连接失败
|
||||
export const websocketonerror = () => {
|
||||
websocket.onerror = function (e: any) {
|
||||
console.log('连接 websocket 失败', e);
|
||||
};
|
||||
};
|
||||
|
||||
// socket 断开链接
|
||||
export const websocketclose = () => {
|
||||
websocket.onclose = function (e: any) {
|
||||
console.log('断开连接', e);
|
||||
};
|
||||
};
|
||||
|
||||
// socket 重置心跳
|
||||
export const resetHeart = () => {
|
||||
socketHeart = 0;
|
||||
socketError = 0;
|
||||
clearInterval(heartTime);
|
||||
sendSocketHeart();
|
||||
};
|
||||
|
||||
// socket心跳发送
|
||||
export const sendSocketHeart = () => {
|
||||
heartTime = setInterval(() => {
|
||||
// 如果连接正常则发送心跳
|
||||
if (websocket.readyState == 1) {
|
||||
// if (socketHeart <= 30) {
|
||||
websocket.send(
|
||||
JSON.stringify({
|
||||
type: 'ping'
|
||||
})
|
||||
);
|
||||
socketHeart = socketHeart + 1;
|
||||
} else {
|
||||
// 重连
|
||||
reconnect();
|
||||
url = url + '?Authorization=Bearer ' + getToken() + '&clientid=' + import.meta.env.VITE_APP_CLIENT_ID
|
||||
useWebSocket(url, {
|
||||
autoReconnect: {
|
||||
// 重连最大次数
|
||||
retries: 3,
|
||||
// 重连间隔
|
||||
delay: 1000,
|
||||
onFailed() {
|
||||
console.log('websocket重连失败');
|
||||
},
|
||||
},
|
||||
heartbeat: {
|
||||
message: JSON.stringify({type: 'ping'}),
|
||||
// 发送心跳的间隔
|
||||
interval: 10000,
|
||||
// 接收到心跳response的超时时间
|
||||
pongTimeout: 2000,
|
||||
},
|
||||
onConnected() {
|
||||
console.log('websocket已经连接');
|
||||
},
|
||||
onDisconnected() {
|
||||
console.log('websocket已经断开');
|
||||
},
|
||||
onMessage: (_, e) => {
|
||||
if (e.data.indexOf('ping') > 0) {
|
||||
return;
|
||||
}
|
||||
useNoticeStore().addNotice({
|
||||
message: e.data,
|
||||
read: false,
|
||||
time: new Date().toLocaleString()
|
||||
});
|
||||
ElNotification({
|
||||
title: '消息',
|
||||
message: e.data,
|
||||
type: 'success',
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
}, HeartTimeOut);
|
||||
};
|
||||
|
||||
// socket重连
|
||||
export const reconnect = () => {
|
||||
if (socketError <= 2) {
|
||||
clearInterval(heartTime);
|
||||
initWebSocket(socketUrl);
|
||||
socketError = socketError + 1;
|
||||
// eslint-disable-next-line prettier/prettier
|
||||
console.log('socket重连', socketError);
|
||||
} else {
|
||||
// eslint-disable-next-line prettier/prettier
|
||||
console.log('重试次数已用完');
|
||||
clearInterval(heartTime);
|
||||
}
|
||||
};
|
||||
|
||||
// socket 发送数据
|
||||
export const sendMsg = (data: any) => {
|
||||
websocket.send(data);
|
||||
};
|
||||
|
||||
// socket 接收数据
|
||||
export const websocketonmessage = () => {
|
||||
websocket.onmessage = function (e: any) {
|
||||
if (e.data.indexOf('heartbeat') > 0) {
|
||||
resetHeart();
|
||||
}
|
||||
if (e.data.indexOf('ping') > 0) {
|
||||
return;
|
||||
}
|
||||
useNoticeStore().addNotice({
|
||||
message: e.data,
|
||||
read: false,
|
||||
time: new Date().toLocaleString()
|
||||
});
|
||||
ElNotification({
|
||||
title: '消息',
|
||||
message: e.data,
|
||||
type: 'success',
|
||||
duration: 3000
|
||||
});
|
||||
return e.data;
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue