@hile/message-ws 1.0.0 → 1.0.2
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 +7 -2
- package/SKILL.md +17 -3
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -6
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -39,6 +39,10 @@ class AppWs extends MessageWs {
|
|
|
39
39
|
return data;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
+
|
|
43
|
+
public request<T = any>(data: T, timeout?: number) {
|
|
44
|
+
return this._send(data, timeout);
|
|
45
|
+
}
|
|
42
46
|
}
|
|
43
47
|
```
|
|
44
48
|
|
|
@@ -79,10 +83,11 @@ ws.on('open', () => {
|
|
|
79
83
|
|------|------|------|
|
|
80
84
|
| `constructor` | `new SubClass(ws: WebSocket)` | 传入已连接的 WebSocket 实例 |
|
|
81
85
|
| `exec` | `protected abstract exec(data: any): Promise<any>` | 子类实现:处理对端请求 |
|
|
82
|
-
| `
|
|
86
|
+
| `_send` | `protected _send<T>(data: T, timeout?: number)` | 发送双向请求(`twoway: true`),返回 `{ abort, response }`。子类自行暴露为 public |
|
|
87
|
+
| `_push` | `protected _push<T>(data: T, timeout?: number)` | 发送单向推送(`twoway: false`),接收方不回复 RESPONSE |
|
|
83
88
|
| `dispose` | `dispose(): void` | 移除消息监听,释放资源 |
|
|
84
89
|
|
|
85
|
-
### `
|
|
90
|
+
### `_send` 返回值
|
|
86
91
|
|
|
87
92
|
| 属性 | 类型 | 说明 |
|
|
88
93
|
|------|------|------|
|
package/SKILL.md
CHANGED
|
@@ -38,7 +38,14 @@ abstract class MessageWs extends MessageModem {
|
|
|
38
38
|
|
|
39
39
|
protected abstract exec(data: any): Promise<any>;
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
// 发送双向请求(protected,子类自行暴露)
|
|
42
|
+
protected _send<T = any>(data: T, timeout?: number): {
|
|
43
|
+
abort: () => void;
|
|
44
|
+
response: <U = any>() => Promise<U>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// 发送单向推送(protected,子类自行暴露)
|
|
48
|
+
protected _push<T = any>(data: T, timeout?: number): {
|
|
42
49
|
abort: () => void;
|
|
43
50
|
response: <U = any>() => Promise<U>;
|
|
44
51
|
};
|
|
@@ -63,6 +70,10 @@ class MyWs extends MessageWs {
|
|
|
63
70
|
default: return data;
|
|
64
71
|
}
|
|
65
72
|
}
|
|
73
|
+
|
|
74
|
+
public request<T = any>(data: T, timeout?: number) {
|
|
75
|
+
return this._send(data, timeout);
|
|
76
|
+
}
|
|
66
77
|
}
|
|
67
78
|
```
|
|
68
79
|
|
|
@@ -120,8 +131,11 @@ ws.on('open', () => {
|
|
|
120
131
|
// ❌ 不能直接实例化
|
|
121
132
|
const modem = new MessageWs(ws); // abstract
|
|
122
133
|
|
|
123
|
-
// ✅ 继承并实现 exec
|
|
124
|
-
class MyWs extends MessageWs {
|
|
134
|
+
// ✅ 继承并实现 exec,自行暴露 _send
|
|
135
|
+
class MyWs extends MessageWs {
|
|
136
|
+
protected async exec(data: any) { return data; }
|
|
137
|
+
public request<T = any>(data: T, timeout?: number) { return this._send(data, timeout); }
|
|
138
|
+
}
|
|
125
139
|
|
|
126
140
|
// ❌ 传输非 JSON 安全的数据
|
|
127
141
|
modem.request(new Map()); // Map 序列化会丢失
|
package/dist/index.d.ts
CHANGED
|
@@ -25,13 +25,6 @@ export declare abstract class MessageWs extends MessageModem {
|
|
|
25
25
|
private readonly listener;
|
|
26
26
|
constructor(ws: WebSocket);
|
|
27
27
|
protected post<T = any>(data: MessageTransferFormat<T>): void;
|
|
28
|
-
/**
|
|
29
|
-
* 向对端发送请求
|
|
30
|
-
*/
|
|
31
|
-
request<T = any>(data: T, timeout?: number): {
|
|
32
|
-
abort: () => void;
|
|
33
|
-
response: <U = any>() => Promise<U>;
|
|
34
|
-
};
|
|
35
28
|
/**
|
|
36
29
|
* 移除消息监听,释放资源
|
|
37
30
|
*/
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hile/message-ws",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"vitest": "^4.0.18"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@hile/message-modem": "^1.0.
|
|
26
|
+
"@hile/message-modem": "^1.0.3",
|
|
27
27
|
"ws": "^8.19.0"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "0e351166b8351d21e1a97543bc8583ef9513fb67"
|
|
30
30
|
}
|