@wuisdk/plugin-wss 0.1.0
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/README.md +1 -0
- package/dist/hub.worker.d.ts +1 -0
- package/dist/hub.worker.js +1 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +1 -0
- package/package.json +30 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
## @wuisdk/plugin-wss
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
let e=null,t,n;self.onmessage=t=>{let{head:n,data:i}=t.data;n==`INIT`?r(i):e?.readyState===WebSocket.OPEN&&e.send(JSON.stringify(t.data))};const r=o=>{let{url:s,app:c,uid:l,ver:u,rel:d}=o;if(!s||!c||!l||!u||!d){console.error(`[HubWorker] Invalid parameters`);return}let f=`${s}?app=${c}&uid=${l}&ver=${u}&rel=${d}`;e&&(e.onopen=null,e.onclose=null,e.onerror=null,e.onmessage=null,e.close()),e=new WebSocket(f),e.onopen=()=>{self.postMessage({head:`SYS_CONNECTED`})},e.onclose=()=>{self.postMessage({head:`DISCON`}),clearInterval(t),clearTimeout(n),n=self.setTimeout(()=>r(o),a())},e.onerror=e=>{console.error(`[HubWorker] WebSocket Error`,e)},e.onmessage=e=>{if(typeof e.data==`string`)try{let t=JSON.parse(e.data);t.head===`ACCEPT`&&i(),self.postMessage(t)}catch(e){console.error(`[HubWorker] Parse Error:`,e)}}},i=()=>{clearInterval(t),t=setInterval(()=>{e&&e.readyState===WebSocket.OPEN&&e.send(JSON.stringify({head:`PING`}))},1e3*50)},a=()=>Math.floor(Math.random()*20001)+1e4;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { WuiContext, WuiPlugin } from "@wuisdk/core";
|
|
2
|
+
//#region src/types.d.ts
|
|
3
|
+
declare module '@wuisdk/core' {
|
|
4
|
+
interface WuiPluginMap {
|
|
5
|
+
readonly wss: IWssService;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
declare module '@wuisdk/core' {
|
|
9
|
+
interface WuiEventMap {
|
|
10
|
+
'wss:connected': void;
|
|
11
|
+
'wss:disconnected': void;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
interface WssPluginOptions {
|
|
15
|
+
worker: new () => Worker;
|
|
16
|
+
endpoint?: string;
|
|
17
|
+
}
|
|
18
|
+
interface IWssService {
|
|
19
|
+
post(packet: WSPacket): void;
|
|
20
|
+
subscribe<T>(topic: WSTopic, callback: (data: T) => void, signal?: AbortSignal): () => void;
|
|
21
|
+
}
|
|
22
|
+
type WSTopic = 'SERVER' | 'CLIENT' | 'BRDMSG';
|
|
23
|
+
interface WSPacket<T = unknown> {
|
|
24
|
+
head: string;
|
|
25
|
+
data?: T;
|
|
26
|
+
}
|
|
27
|
+
interface WSParams {
|
|
28
|
+
url: string;
|
|
29
|
+
app: string;
|
|
30
|
+
uid: string;
|
|
31
|
+
ver: string;
|
|
32
|
+
rel: string;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/service.d.ts
|
|
36
|
+
declare class WuiWssPlugin implements WuiPlugin {
|
|
37
|
+
private options?;
|
|
38
|
+
readonly name = "wui-wss-plugin";
|
|
39
|
+
private service;
|
|
40
|
+
constructor(options?: WssPluginOptions | undefined);
|
|
41
|
+
install(context: WuiContext): void;
|
|
42
|
+
init(): void;
|
|
43
|
+
destroy(): void;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/index.d.ts
|
|
47
|
+
declare const info: () => {
|
|
48
|
+
version: string;
|
|
49
|
+
name: string;
|
|
50
|
+
};
|
|
51
|
+
//#endregion
|
|
52
|
+
export { type IWssService, type WSPacket, type WSParams, type WSTopic, type WssPluginOptions, WuiWssPlugin, info };
|
package/dist/index.js
ADDED
|
@@ -0,0 +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();break}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.ctx.ui.toast.success(`เชื่อมต่อ WssHub สำเร็จ.!`)}handleDisconnect(){this.ctx.events.emit(`wss:disconnected`),this.ctx.ui.toast.error(`ขาดการเชื่อมต่อ WssHub.!`)}};const n=()=>({version:`0.1.0`,name:`@wuisdk/plugin-wss`.split(`/`)[1]});export{e as WuiWssPlugin,n as info};
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wuisdk/plugin-wss",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"import": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts"
|
|
9
|
+
},
|
|
10
|
+
"./hub.worker": {
|
|
11
|
+
"import": "./dist/hub.worker.js",
|
|
12
|
+
"types": "./dist/hub.worker.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsdown --minify",
|
|
23
|
+
"dev": "tsdown --no-clean --watch"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@wuisdk/core": "*"
|
|
27
|
+
},
|
|
28
|
+
"author": "hs3lcs@gmail.com",
|
|
29
|
+
"license": "MIT"
|
|
30
|
+
}
|