@maiyunnet/kebab 3.1.14 → 3.1.15
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/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/ws.d.ts +1 -0
- package/lib/ws.js +9 -26
- package/package.json +1 -1
- package/www/example/ws/main.d.ts +5 -0
- package/www/example/ws/main.js +15 -0
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* ------------------------
|
|
10
10
|
*/
|
|
11
11
|
/** --- 当前系统版本号 --- */
|
|
12
|
-
export const VER = '3.1.
|
|
12
|
+
export const VER = '3.1.15';
|
|
13
13
|
// --- 服务端用的路径 ---
|
|
14
14
|
const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
|
|
15
15
|
/** --- /xxx/xxx --- */
|
package/lib/ws.d.ts
CHANGED
|
@@ -97,6 +97,7 @@ export declare class Socket {
|
|
|
97
97
|
/** --- 绑定监听 --- */
|
|
98
98
|
on(event: 'message', cb: (msg: {
|
|
99
99
|
'opcode': EOpcode;
|
|
100
|
+
'buffer': Buffer;
|
|
100
101
|
'data': Buffer | string;
|
|
101
102
|
}) => void | Promise<void>): this;
|
|
102
103
|
on(event: 'error', cb: (error: any) => void | Promise<void>): this;
|
package/lib/ws.js
CHANGED
|
@@ -31,6 +31,7 @@ export class Socket {
|
|
|
31
31
|
this._close = false;
|
|
32
32
|
/** --- 未绑定自定义监听事件的默认执行函数 --- */
|
|
33
33
|
this._on = {
|
|
34
|
+
/** --- 消息 --- */
|
|
34
35
|
message: (msg) => {
|
|
35
36
|
this._waitMsg.push(msg);
|
|
36
37
|
},
|
|
@@ -135,28 +136,18 @@ export class Socket {
|
|
|
135
136
|
}
|
|
136
137
|
/** --- 创建成功后第一时间绑定事件 --- */
|
|
137
138
|
_bindEvent() {
|
|
138
|
-
this._ws.on('message',
|
|
139
|
+
this._ws.on('message', msg => {
|
|
139
140
|
(async () => {
|
|
140
141
|
if (msg.opcode === EOpcode.CLOSE) {
|
|
141
142
|
return;
|
|
142
143
|
}
|
|
143
|
-
|
|
144
|
-
if ('data' in msg) {
|
|
145
|
-
data = Buffer.concat(msg.data);
|
|
146
|
-
if (msg.opcode === EOpcode.TEXT) {
|
|
147
|
-
data = data.toString();
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
data = await (msg.opcode === EOpcode.TEXT ? msg.toString() : msg.toBuffer());
|
|
152
|
-
}
|
|
144
|
+
const buf = 'data' in msg ? Buffer.concat(msg.data) : await msg.toBuffer();
|
|
153
145
|
this._on.message({
|
|
154
146
|
'opcode': msg.opcode,
|
|
155
|
-
'
|
|
147
|
+
'buffer': buf,
|
|
148
|
+
'data': msg.opcode === EOpcode.TEXT ? buf.toString() : buf,
|
|
156
149
|
});
|
|
157
|
-
})().catch(() => {
|
|
158
|
-
// --- nothing ---
|
|
159
|
-
});
|
|
150
|
+
})().catch(() => { });
|
|
160
151
|
}).on('drain', () => {
|
|
161
152
|
this._on.drain();
|
|
162
153
|
}).on('error', (e) => {
|
|
@@ -292,11 +283,7 @@ function bindPipe(s1, s2) {
|
|
|
292
283
|
switch (msg.opcode) {
|
|
293
284
|
case EOpcode.TEXT:
|
|
294
285
|
case EOpcode.BINARY: {
|
|
295
|
-
|
|
296
|
-
s2.writeText(msg.data);
|
|
297
|
-
break;
|
|
298
|
-
}
|
|
299
|
-
s2.writeBinary(msg.data);
|
|
286
|
+
s2.writeBinary(msg.buffer);
|
|
300
287
|
break;
|
|
301
288
|
}
|
|
302
289
|
case EOpcode.CLOSE: {
|
|
@@ -325,11 +312,7 @@ function bindPipe(s1, s2) {
|
|
|
325
312
|
switch (msg.opcode) {
|
|
326
313
|
case EOpcode.TEXT:
|
|
327
314
|
case EOpcode.BINARY: {
|
|
328
|
-
|
|
329
|
-
s1.writeText(msg.data);
|
|
330
|
-
break;
|
|
331
|
-
}
|
|
332
|
-
s1.writeBinary(msg.data);
|
|
315
|
+
s1.writeBinary(msg.buffer);
|
|
333
316
|
break;
|
|
334
317
|
}
|
|
335
318
|
case EOpcode.CLOSE: {
|
|
@@ -426,7 +409,7 @@ export async function rsocket(ctr, host, port) {
|
|
|
426
409
|
switch (msg.opcode) {
|
|
427
410
|
case EOpcode.TEXT:
|
|
428
411
|
case EOpcode.BINARY: {
|
|
429
|
-
socket.write(msg.
|
|
412
|
+
socket.write(msg.buffer);
|
|
430
413
|
break;
|
|
431
414
|
}
|
|
432
415
|
case EOpcode.CLOSE: {
|
package/package.json
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as sCtr from '#kebab/sys/ctr.js';
|
|
2
|
+
import * as lCore from '#kebab/lib/core.js';
|
|
3
|
+
export default class extends sCtr.Ctr {
|
|
4
|
+
onLoad() {
|
|
5
|
+
lCore.debug('WebSocket test main onLoad.');
|
|
6
|
+
setTimeout(() => {
|
|
7
|
+
this._writeText('Hello, world!');
|
|
8
|
+
this._end();
|
|
9
|
+
}, 2_000);
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
onClose() {
|
|
13
|
+
lCore.debug('WebSocket test main onClose');
|
|
14
|
+
}
|
|
15
|
+
}
|