import { x_SSEClient } from "x_sse_s"; import {xSSEOptions} from "../interface.uts" export class SSEClientApp { private eventSource : x_SSEClient | null = null; private url : string; private onOpenEvt = () => { } private onErrorEvt = () => { } private onClosedEvt = () => { } private onMessageEvt = (data : string) => { } private headers:Map constructor(options:xSSEOptions) { this.url = options.url; const headersOpts = options?.header??({} as UTSJSONObject) this.headers = headersOpts.toMap() } // 打开连接 public connect() : void { if (this.eventSource||this.isConnected()) { console.error('已有打开的,不允许重复'); return; } this.eventSource = new x_SSEClient(this.url,this.headers); this.eventSource.onOpen(this.onOpenEvt) this.eventSource.onError(this.onErrorEvt) this.eventSource.onClosed(this.onClosedEvt) this.eventSource.onMessage(this.onMessageEvt) this.eventSource.connect(); } onOpen(callback:()=>void){ this.onOpenEvt = callback } onError(callback:()=>void){ this.onErrorEvt = callback } onClosed(callback:()=>void){ this.onClosedEvt = callback } onMessage(callback:(data:string)=>void){ this.onMessageEvt = callback } // 关闭连接 public disconnect() : void { if (!this.eventSource) { return; } this.eventSource.disconnect(); this.onClosedEvt() this.eventSource = null; } isConnected():boolean { return this.eventSource?.isConnected()??false } }