From 415db453e41054dd42b895ea43191be8c5a1cef7 Mon Sep 17 00:00:00 2001 From: fan Date: Mon, 19 Jun 2023 11:14:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A1=86=E6=9E=B6=E4=BB=A3=E7=A0=81=E5=90=8C?= =?UTF-8?q?=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- graphic-pixi | 2 +- src/jl-graphic/message/WsMsgBroker.ts | 55 +++++++++++++-------------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/graphic-pixi b/graphic-pixi index ff5ed78..533ebdc 160000 --- a/graphic-pixi +++ b/graphic-pixi @@ -1 +1 @@ -Subproject commit ff5ed78027861c0af12c5bcfb8c073e362d282b0 +Subproject commit 533ebdc9a89ac1e2a6e5d9f3fbdc98c9f7d416c8 diff --git a/src/jl-graphic/message/WsMsgBroker.ts b/src/jl-graphic/message/WsMsgBroker.ts index dc172f8..8b8cd53 100644 --- a/src/jl-graphic/message/WsMsgBroker.ts +++ b/src/jl-graphic/message/WsMsgBroker.ts @@ -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; - } } /**