This commit is contained in:
2026-09-24 16:25:22 +08:00
commit 7428184f01
1198 changed files with 314515 additions and 0 deletions
@@ -0,0 +1,5 @@
{
"dependencies": {
"x_sse_s": "./lib/x_sse_s.har"
}
}
@@ -0,0 +1,55 @@
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<string,string>
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
}
}