框架代码同步

This commit is contained in:
fan 2023-06-19 11:14:54 +08:00
parent a28dad6c03
commit 415db453e4
2 changed files with 27 additions and 30 deletions

@ -1 +1 @@
Subproject commit ff5ed78027861c0af12c5bcfb8c073e362d282b0
Subproject commit 533ebdc9a89ac1e2a6e5d9f3fbdc98c9f7d416c8

View File

@ -9,8 +9,19 @@ import type { GraphicApp } from '../app/JlGraphicApp';
import { GraphicState } from '../core/JlGraphic';
export interface StompCliOption {
wsUrl: string; // websocket url
token: string; // 认证token
/**
* websocket url地址
*/
wsUrl: string;
/**
* token
*/
token?: string;
/**
*
* @returns
*/
onAuthenticationFailed?: () => void;
reconnectDelay?: number; // 重连延时默认3秒
heartbeatIncoming?: number; // 服务端过来的心跳间隔默认30秒
heartbeatOutgoing?: number; // 到服务端的心跳间隔默认30秒
@ -32,7 +43,7 @@ export class StompCli {
private static connected = false;
static new(options: StompCliOption) {
if (StompCli.enabled) {
// 以及启用
// 已经启用
return;
// throw new Error('websocket 已连接若确实需要重新连接请先断开StompCli.close再重新StompCli.new')
}
@ -41,12 +52,8 @@ export class StompCli {
StompCli.client = new StompClient({
brokerURL: StompCli.options.wsUrl,
connectHeaders: {
Authorization: StompCli.options.token,
// Authorization: ''
Authorization: StompCli.options.token ? StompCli.options.token : '',
},
// debug: (str) => {
// console.log(str)
// }
reconnectDelay: StompCli.options.reconnectDelay,
heartbeatIncoming: StompCli.options.heartbeatIncoming,
heartbeatOutgoing: StompCli.options.heartbeatOutgoing,
@ -61,19 +68,21 @@ export class StompCli {
};
StompCli.client.onStompError = (frame: Frame) => {
console.error(
'Stomp收到error消息,可能是认证失败(暂时没有判断具体错误类型,后需添加判断),关闭Stomp客户端',
frame
);
StompCli.close();
const errMsg = frame.headers['message'];
if (errMsg === '401') {
console.warn('认证失败,断开WebSocket连接');
StompCli.close();
if (StompCli.options.onAuthenticationFailed) {
StompCli.options.onAuthenticationFailed();
}
} else {
console.error('收到Stomp错误消息', frame);
}
};
StompCli.client.onDisconnect = (frame: Frame) => {
console.log('Stomp 断开连接', frame);
StompCli.connected = false;
// StompCli.appMsgBroker.forEach(broker => {
// broker.close();
// });
};
StompCli.client.onWebSocketClose = (evt: CloseEvent) => {
console.log('websocket 关闭', evt);
@ -82,9 +91,6 @@ export class StompCli {
// websocket错误处理
StompCli.client.onWebSocketError = (err: Event) => {
console.log('websocket错误', err);
// StompCli.appMsgBroker.forEach(broker => {
// broker.unsbuscribeAll();
// });
};
StompCli.client.activate();
@ -140,19 +146,10 @@ export class StompCli {
// 状态订阅消息转换器
export type MessageConverter = (message: Uint8Array) => GraphicState[];
// 图形app状态订阅
export class AppStateSubscription {
export interface AppStateSubscription {
destination: string;
messageConverter: MessageConverter;
subscription?: StompSubscription; // 订阅成功对象,用于取消订阅
constructor(
destination: string,
messageConverter: MessageConverter,
subscription?: StompSubscription
) {
this.destination = destination;
this.messageConverter = messageConverter;
this.subscription = subscription;
}
}
/**