diff --git a/src/common/ClientConnection.ts b/src/common/ClientConnection.ts index d158aab..762964f 100644 --- a/src/common/ClientConnection.ts +++ b/src/common/ClientConnection.ts @@ -38,6 +38,9 @@ export default class ClientConnection { user_name?: string | null, password?: string | null ) { + if (this._destoryed) { + return; + } this.url = url; this.user_name = user_name ?? ""; this.password = password ?? ""; @@ -77,13 +80,36 @@ export default class ClientConnection { onClose(ev: CloseEvent) { this._is_login = false; console.log("onClose"); + this._reconnect(); + EventBus.getInstance().emit(EventNamesDefine.WebSocketClose, this); } onError(ev: Event) { this._is_login = false; - setTimeout(() => { - this.reconnectTo(this.url, this.user_name, this.password); - }, 5000); + this._reconnect(); + EventBus.getInstance().emit(EventNamesDefine.WebSocketError, this); + } + + private _reconnectTimer: ReturnType | null = null; + + private _reconnect() { + if (this._destoryed) { + return; + } + if ( + !this.ws || + (this.ws.readyState != WebSocket.CONNECTING && + this.ws.readyState != WebSocket.OPEN) + ) { + if (this._reconnectTimer) { + clearTimeout(this._reconnectTimer); + this._reconnectTimer = null; + } + this._reconnectTimer = setTimeout(() => { + console.log("reconnection...", new Date().getSeconds()); + this.reconnectTo(this.url, this.user_name, this.password); + }, 3000); + } } onOpen(ev: Event) { @@ -111,6 +137,12 @@ export default class ClientConnection { if (login_response) { this._is_login = !login_response.has_exception && login_response.success; + if (this.is_login) { + EventBus.getInstance().emit( + EventNamesDefine.WebSocketConnected, + this + ); + } if ( this.login_callback && typeof this.login_callback == "function" @@ -118,8 +150,7 @@ export default class ClientConnection { this.login_callback(this._is_login); } } - } - if (this.rpc_map.has(packet.rpc_id)) { + } else if (this.rpc_map.has(packet.rpc_id)) { const f = this.rpc_map.get(packet.rpc_id); if (f && typeof f == "function") { f(false, packet, ev.data); @@ -232,4 +263,15 @@ export default class ClientConnection { JSON.stringify(new Protocol.CloseWindowRequestEntity(window_id)) ); } + + public openWindow(data: Protocol.OpenWindowRequestEntity) { + this.ws?.send(JSON.stringify(data)); + } + + private _destoryed = false; + public destory() { + this._destoryed = true; + this.ws?.close(); + this.ws = null; + } } diff --git a/src/common/EventBus.ts b/src/common/EventBus.ts index c24651d..beacb8e 100644 --- a/src/common/EventBus.ts +++ b/src/common/EventBus.ts @@ -12,7 +12,12 @@ export default class EventBus extends EventEmitter { } export namespace EventNamesDefine { - export const UnSelectAllWindows = "unselect_all_windows"; - export const WindowResize = "windowResize"; + export const UnSelectAllWindows = "onUnSelectAllWindows"; + export const WindowResize = "onWindowResize"; export const ResponseMessage = "onResponseData"; + export const WebSocketClose = "onWebSocketClose"; + export const WebSocketError = "onWebSocketError"; + export const WebSocketConnected = "onWebSocketConnected"; + export const CurrentConnectDisconnect = "onCurrentConnectDisconnect"; + export const CurrentConnectConnected = "onCurrentConnectConnected"; } diff --git a/src/common/GlobalData.ts b/src/common/GlobalData.ts index 5ef81b1..6eca07b 100644 --- a/src/common/GlobalData.ts +++ b/src/common/GlobalData.ts @@ -2,11 +2,11 @@ import { SessionStorage } from "quasar"; import ApplicationConfigEntity from "src/entities/ApplicationConfigEntity"; import { SignalSourceEntity } from "src/entities/SignalSourceEntity"; import ClientConnection from "./ClientConnection"; +import EventBus, { EventNamesDefine } from "./EventBus"; export default class GlobalData { getSignalSource(uuid: string) { return this.signal_source.find((item) => item && item.uuid == uuid); - throw new Error("Method not implemented."); } private static _instance: GlobalData | null = null; @@ -70,6 +70,43 @@ export default class GlobalData { new ClientConnection(url, user_name, password) ); } + + EventBus.getInstance().on(EventNamesDefine.WebSocketClose, (connection) => + this._onWebSocketClose(connection) + ); + EventBus.getInstance().on(EventNamesDefine.WebSocketError, (connection) => + this._onWebSocketError(connection) + ); + + EventBus.getInstance().on( + EventNamesDefine.WebSocketConnected, + (connection) => this._onWebSocketConnected(connection) + ); + } + + private _onWebSocketClose(connection: ClientConnection) { + if (this.getCurrentClient() == connection) { + EventBus.getInstance().emit( + EventNamesDefine.CurrentConnectDisconnect, + connection + ); + } + } + private _onWebSocketError(connection: ClientConnection) { + if (this.getCurrentClient() == connection) { + EventBus.getInstance().emit( + EventNamesDefine.CurrentConnectDisconnect, + connection + ); + } + } + private _onWebSocketConnected(connection: ClientConnection) { + if (this.getCurrentClient() == connection) { + EventBus.getInstance().emit( + EventNamesDefine.CurrentConnectConnected, + connection + ); + } } public getClient(name: string | null) { diff --git a/src/entities/WSProtocol.ts b/src/entities/WSProtocol.ts index 1dfcd0a..e226fe5 100644 --- a/src/entities/WSProtocol.ts +++ b/src/entities/WSProtocol.ts @@ -223,9 +223,20 @@ export namespace Protocol { y: number = 0; width: number = 0; height: number = 0; - constructor() { + constructor( + signal_source: string, + x: number, + y: number, + width: number, + height: number + ) { super(); this.command = Commands.kOpenWindow; + this.signal_source = signal_source ?? ""; + this.x = x ?? 0; + this.y = y ?? 0; + this.width = width ?? 0; + this.height = height ?? 0; } } diff --git a/src/i18n/zh-CN/index.ts b/src/i18n/zh-CN/index.ts index 1ff314c..487c02f 100644 --- a/src/i18n/zh-CN/index.ts +++ b/src/i18n/zh-CN/index.ts @@ -10,7 +10,11 @@ export default { password: "密码", "please input user name": "请输入用户", "please input password": "请输入密码", - "login fail!": "登陆失败", + "login fail!": "登陆失败!", + "user or password error!": "用户名或密码错误!", + "connect time out!": "连接超时", + "please check server state, or check server ip address!": + "请检查服务器状态,或者检查服务器IP地址是否正确!", "Please type something": "请输入内容", "grid setting": "宫格设置", @@ -23,4 +27,7 @@ export default { "close all windwos": "关闭所有窗口", "close this window": "关闭当前窗口", "close other windows": "关闭其它窗口", + + "network disconnect!": "网络连接断开!", + "wait reconnection": "等待重连中", }; diff --git a/src/pages/Index.vue b/src/pages/Index.vue index 29061fe..174b19d 100644 --- a/src/pages/Index.vue +++ b/src/pages/Index.vue @@ -5,13 +5,29 @@