From 0462ff6eed47a48ae85ce288c61319fea4d2920e Mon Sep 17 00:00:00 2001 From: wuzuowei Date: Wed, 5 Jun 2024 18:07:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0websocet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/microhost/components/rightInfo/ws.ts | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 platform-code/src/microhost/components/rightInfo/ws.ts diff --git a/platform-code/src/microhost/components/rightInfo/ws.ts b/platform-code/src/microhost/components/rightInfo/ws.ts new file mode 100644 index 0000000..a1e8b4a --- /dev/null +++ b/platform-code/src/microhost/components/rightInfo/ws.ts @@ -0,0 +1,163 @@ + +/** + * websocket配置项类 + * author: frez02@126.com + * datetime: 2021-01-02 + */ +export class WebSocketOption { + /** + * 是否自动启动。开启后,遇error将会自动重启连接 + */ + autoStart: boolean = false + /** + * 心跳数据包 + */ + heart: string | false = false + /** + * WebSocket服务地址 + */ + ws: string | null = null //ws://170.0.35.145:12781/rolling/wsnotify + /** + * 连接模式 + */ + type: "TCP" = "TCP" + + //以下为回调函数 + /** + * onopen + */ + onOpen?: Function + /** + * onmessage + */ + onMessage?: Function + /** + * onclose + */ + onClose?: Function + /** + * onerror + */ + onError?: Function +} + +/** + * websocket类 + * author: frez02@126.com + * datetime: 2020-01-02 + */ +export class WebSocketLoader { + protected server : WebSocket | null = null + protected option : WebSocketOption + protected disposed : boolean = false + protected paused : boolean = false + + constructor(opts : WebSocketOption) { + this.option = opts + + if(opts.autoStart) { + this.start() + } + } + + /** + * 开启连接 + */ + start = () => { + this.restart() + } + + /** + * 重新开始连接 + */ + restart = () => { + this.disposed = false + this.paused = false + this.connect() + } + + /** + * 暂停接收 + */ + pause = () => { + this.paused = true + } + + /** + * 恢复接收 + */ + resume = () => { + this.paused = false + } + + protected connect = () => { + if(this.server == null){ + this.server = new WebSocket(this.option.ws) + } + else if(this.server.readyState == 1){ + this.server.close() + return + } + else if(this.server.readyState == 2){ + return + } + else { + //0 - 尚未建立连接, 3 - 已经关闭或者已经不能打开 + } + + this.server.onclose = this.onClose + this.server.onopen = this.onOpen + this.server.onmessage = this.onMessage + this.server.onerror = this.onError + } + + /** + * 释放连接 + */ + dispose = () => { + this.disposed = true + + if(this.server == null){ + return + } + + if(this.server.readyState == 2) { + this.server.close() + } + } + + /** + * 连接是否活动状态 + */ + isLive = () => { + return this.server == null ? false : (this.disposed == true ? false : this.server.readyState == 1) + } + + protected onOpen = (e : any) => { + if(this.option.onOpen){ + this.option.onOpen() + } + } + + protected onClose = (e : any) => { + if(this.option.onClose){ + this.option.onClose() + } + } + + protected onMessage = (e : any) => { + if(this.option.onMessage){ + this.option.onMessage(e.data) + } + } + + protected onError = (e : any) => { + if(this.option.autoStart && this.disposed == false){ + this.restart() + } + + if(this.option.onError){ + this.option.onError() + } + } +}