@wuisdk/plugin-wss 0.1.1 → 0.1.3
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 +18 -8
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -19,11 +19,6 @@ interface IWssService {
|
|
|
19
19
|
post(packet: WSPacket): void;
|
|
20
20
|
subscribe<T>(topic: WSTopic, callback: (data: T) => void, signal?: AbortSignal): () => void;
|
|
21
21
|
}
|
|
22
|
-
type WSTopic = 'SERVER' | 'CLIENT' | 'BRDMSG';
|
|
23
|
-
interface WSPacket<T = unknown> {
|
|
24
|
-
head: string;
|
|
25
|
-
data?: T;
|
|
26
|
-
}
|
|
27
22
|
interface WSParams {
|
|
28
23
|
url: string;
|
|
29
24
|
app: string;
|
|
@@ -31,9 +26,24 @@ interface WSParams {
|
|
|
31
26
|
ver: string;
|
|
32
27
|
rel: string;
|
|
33
28
|
}
|
|
29
|
+
type WSTopic = 'SERVER' | 'CLIENT' | 'BRDMSG';
|
|
30
|
+
interface WSPacket<T = unknown> {
|
|
31
|
+
head: string;
|
|
32
|
+
data?: T;
|
|
33
|
+
}
|
|
34
|
+
interface WSBrdPacket<T = unknown> {
|
|
35
|
+
from: string;
|
|
36
|
+
to: string;
|
|
37
|
+
cmd: string;
|
|
38
|
+
msg: T;
|
|
39
|
+
}
|
|
40
|
+
interface WSTimePacket {
|
|
41
|
+
time: string;
|
|
42
|
+
unix: number;
|
|
43
|
+
}
|
|
34
44
|
//#endregion
|
|
35
45
|
//#region src/service.d.ts
|
|
36
|
-
declare class WuiWssPlugin implements WuiPlugin {
|
|
46
|
+
export declare class WuiWssPlugin implements WuiPlugin {
|
|
37
47
|
private options?;
|
|
38
48
|
readonly name = "wui-wss-plugin";
|
|
39
49
|
private service;
|
|
@@ -44,9 +54,9 @@ declare class WuiWssPlugin implements WuiPlugin {
|
|
|
44
54
|
}
|
|
45
55
|
//#endregion
|
|
46
56
|
//#region src/index.d.ts
|
|
47
|
-
declare const info: () => {
|
|
57
|
+
export declare const info: () => {
|
|
48
58
|
version: string;
|
|
49
59
|
name: string;
|
|
50
60
|
};
|
|
51
61
|
//#endregion
|
|
52
|
-
export {
|
|
62
|
+
export type { IWssService, WSBrdPacket, WSPacket, WSParams, WSTimePacket, WSTopic, WssPluginOptions };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=class{options;name=`wui-wss-plugin`;service;constructor(e){this.options=e}install(e){this.service=new t(e,this.options),e.registerPlugin(`wss`,this.service)}init(){this.service.init()}destroy(){this.service.destroy()}},t=class{ctx;options;abortController=new AbortController;worker=null;wsuuid=null;subscribers=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()}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:8855/ws`,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}post(e){if(!this.worker){console.warn(`[WssService] Worker not initialized`);return}this.worker.postMessage(e)}subscribe(e,t,n){this.subscribers.has(e)||(this.subscribers.set(e,new Set),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.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.
|
|
1
|
+
var e=class{options;name=`wui-wss-plugin`;service;constructor(e){this.options=e}install(e){this.service=new t(e,this.options),e.registerPlugin(`wss`,this.service)}init(){this.service.init()}destroy(){this.service.destroy()}},t=class{ctx;options;abortController=new AbortController;worker=null;wsuuid=null;subscribers=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`BRDMSG`:this.handleBroadcast(n);break;case`VERIFY`:console.log(`🐸`,t,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:8855/ws`,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}post(e){if(!this.worker){console.warn(`[WssService] Worker not initialized`);return}this.worker.postMessage(e)}subscribe(e,t,n){this.subscribers.has(e)||(this.subscribers.set(e,new Set),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.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()}handleDisconnect(){this.ctx.events.emit(`wss:disconnected`)}handleBroadcast(e){let t=e.to===this.wsuuid||e.to==`@all`;e.cmd===`LOGS`&&console.log(e.msg),e.cmd===`CONRST`&&t&&(sessionStorage.clear(),window.location.reload()),e.cmd===`LOGOUT`&&t&&(localStorage.clear(),sessionStorage.clear(),window.location.reload()),e.cmd===`RESULT`&&t&&(console.log(e.cmd,`From:`,e.from),console.log(e.msg))}verifyToken(){let{storage:e}=this.ctx.config,t=localStorage.getItem(e.AUTH_TOKEN);t&&this.post({head:`VERIFY`,data:t})}};const n=()=>({version:`0.1.3`,name:`@wuisdk/plugin-wss`.split(`/`)[1]});export{e as WuiWssPlugin,n as info};
|