@wuisdk/plugin-wss 0.1.10 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +19 -9
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ interface WssPluginOptions {
|
|
|
19
19
|
interface IWssService {
|
|
20
20
|
readonly wsuuid: string;
|
|
21
21
|
post(packet: WSPacket): void;
|
|
22
|
+
direct(target: string, payload: unknown): void;
|
|
23
|
+
publish(topic: WSTopic, payload: unknown): void;
|
|
22
24
|
subscribe<T>(topic: WSTopic, callback: (data: T) => void, signal?: AbortSignal): () => void;
|
|
23
25
|
}
|
|
24
26
|
interface WSParams {
|
|
@@ -28,22 +30,30 @@ interface WSParams {
|
|
|
28
30
|
ver: string;
|
|
29
31
|
rel: string;
|
|
30
32
|
}
|
|
31
|
-
type WSTopic = '
|
|
33
|
+
type WSTopic = 'SRV_TIME' | 'SRV_LOGS' | 'SRV_STATUS' | 'CLT_ONLINE' | 'CLT_STATUS';
|
|
32
34
|
interface WSPacket<T = unknown> {
|
|
33
35
|
head: string;
|
|
34
36
|
data?: T;
|
|
35
37
|
}
|
|
36
|
-
interface
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
interface WSPubSub<T = unknown> {
|
|
39
|
+
topic?: string;
|
|
40
|
+
sender?: string;
|
|
41
|
+
target?: string;
|
|
42
|
+
payload?: T;
|
|
41
43
|
}
|
|
42
|
-
interface
|
|
44
|
+
interface WSTimeData {
|
|
43
45
|
time: string;
|
|
44
46
|
unix: number;
|
|
45
47
|
}
|
|
46
|
-
interface
|
|
48
|
+
interface WSLogsData {
|
|
49
|
+
time: string;
|
|
50
|
+
msg: string;
|
|
51
|
+
}
|
|
52
|
+
interface WSCmdPayload {
|
|
53
|
+
sender: string;
|
|
54
|
+
command: string;
|
|
55
|
+
}
|
|
56
|
+
interface WSNotifyData {
|
|
47
57
|
title?: string;
|
|
48
58
|
icon?: string;
|
|
49
59
|
body: string;
|
|
@@ -66,4 +76,4 @@ export declare const info: () => {
|
|
|
66
76
|
name: string;
|
|
67
77
|
};
|
|
68
78
|
//#endregion
|
|
69
|
-
export type { IWssService,
|
|
79
|
+
export type { IWssService, WSCmdPayload, WSLogsData, WSNotifyData, WSPacket, WSParams, WSPubSub, WSTimeData, WSTopic, WssPluginOptions };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"@wuisdk/plugin-auth";var e=`@wuisdk/plugin-wss`,t=`0.1.
|
|
1
|
+
import"@wuisdk/plugin-auth";var e=`@wuisdk/plugin-wss`,t=`0.1.11`,n=class{options;name=`wui-wss-plugin`;service;constructor(e){this.options=e}install(e){this.service=new r(e,this.options),e.registerPlugin(`wss`,this.service)}init(){this.service.init()}destroy(){this.service.destroy()}},r=class{ctx;options;abortController=new AbortController;worker=null;_wsuuid=null;subscribers=/* @__PURE__ */ new Map;constructor(e,t){this.ctx=e,this.options=t}init(){if(!window.Worker){console.warn(`[WssService] Web Workers not supported.`);return}if(!this.options.worker){console.warn(`[WssService] Missing Worker configuration.`);return}let{config:e}=this.ctx,{signal:t}=this.abortController;this.worker=new this.options.worker,this.worker.addEventListener(`message`,e=>{let{head:t,data:n}=e.data;switch(t){case`ACCEPT`:this.handleAccept(n);break;case`DISCON`:this.handleDisconnect();break;case`VERIFY`:console.log(`🐸`,t,n);break;case`KEYEXP`:this.handleKeyExp();break;case`DIRECT`:this.handleDirect(n)}let r=this.subscribers.get(t);r&&r.forEach(e=>e(n))},{signal:t});let n={head:`INIT`,data:{url:this.options.endpoint??`wss://wkrh.info/wss`,app:e.appName,uid:e.appCode,ver:e.version,rel:e.release}};this.worker.postMessage(n)}destroy(){this.abortController.abort(),this.worker?.terminate(),this.worker=null}get wsuuid(){return this._wsuuid??``}post(e){if(!this.worker){console.warn(`[WssService] Worker not initialized`);return}this.worker.postMessage(e)}direct(e,t){let n={head:`DIRECT`,data:{target:e,payload:t}};this.post(n)}publish(e,t){let n={head:e,data:{payload:t}};this.post(n)}subscribe(e,t,n){this.subscribers.has(e)||(this.subscribers.set(e,/* @__PURE__ */ new Set),this._wsuuid&&this.post({head:`SUBSCRIBE`,data:{topic:e}}));let r=this.subscribers.get(e),i=t;r.add(i);let a=!1,o=()=>{if(a)return;a=!0,n&&n.removeEventListener(`abort`,o);let t=this.subscribers.get(e);t&&(t.delete(i),t.size===0&&(this.subscribers.delete(e),this._wsuuid&&this.post({head:`UNSUBSCRIBE`,data:{topic:e}})))};return n&&(n.aborted?o():n.addEventListener(`abort`,o,{once:!0})),o}handleAccept(e){this._wsuuid=e.uuid??``,console.log(`wsuuid:`,this._wsuuid),this.ctx.events.emit(`wss:connected`),this.verifyToken();for(let e of this.subscribers.keys())this.post({head:`SUBSCRIBE`,data:{topic:e}})}handleDisconnect(){this._wsuuid=null,this.ctx.events.emit(`wss:disconnected`)}verifyToken(){let{storage:e}=this.ctx.config,t=localStorage.getItem(e.AUTHKEY);t&&this.post({head:`VERIFY`,data:t})}async handleKeyExp(){if(this.ctx.plugins.auth)try{await this.ctx.plugins.auth.refreshToken(),this.verifyToken()}catch(e){console.warn(`[WssService] Refresh token failed.`,e)}}handleDirect(e){switch(console.log(e),e.command){case`CONRST`:sessionStorage.clear(),window.location.reload();break;case`LOGOUT`:localStorage.clear(),sessionStorage.clear(),window.location.reload()}}};const i=()=>({version:t,name:e.split(`/`)[1]});export{n as WuiWssPlugin,i as info};
|