alemonjs 2.1.0-alpha.7 → 2.1.0-alpha.9
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/lib/app/define-bot.d.ts +17 -0
- package/lib/app/define-bot.js +39 -0
- package/lib/app/event-processor-event.js +1 -1
- package/lib/app/event-processor-middleware.js +1 -1
- package/lib/app/event-processor.js +1 -1
- package/lib/app/hook-use-api.d.ts +9 -2
- package/lib/app/hook-use-api.js +26 -2
- package/lib/app/load.js +1 -1
- package/lib/app/message-api.d.ts +2 -2
- package/lib/cbp/actions.js +9 -6
- package/lib/cbp/api.js +36 -0
- package/lib/cbp/config.js +5 -1
- package/lib/cbp/connect.d.ts +6 -3
- package/lib/cbp/connect.js +68 -32
- package/lib/cbp/index.d.ts +30 -1
- package/lib/cbp/index.js +29 -2
- package/lib/cbp/message.js +9 -0
- package/lib/core/utils.d.ts +77 -0
- package/lib/core/utils.js +162 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -2
- package/lib/jsx.d.ts +142 -0
- package/lib/jsx.js +234 -0
- package/lib/main.js +1 -1
- package/lib/typing/actions.d.ts +1 -1
- package/lib/typing/apis.d.ts +18 -0
- package/lib/typing/client/index.d.ts +1 -1
- package/lib/typing/event/base/user.d.ts +4 -0
- package/lib/typing/message/button.d.ts +1 -1
- package/lib/utils.d.ts +43 -0
- package/lib/utils.js +108 -0
- package/package.json +5 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DefinePlatformFunc } from '../typing/event/index.js'
|
|
2
|
+
import '../global.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 定义机器人
|
|
6
|
+
* @param callback
|
|
7
|
+
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
declare const definePlatform: DefinePlatformFunc
|
|
11
|
+
/**
|
|
12
|
+
* 废弃,请使用 definePlatform
|
|
13
|
+
* @deprecated
|
|
14
|
+
*/
|
|
15
|
+
declare const defineBot: DefinePlatformFunc
|
|
16
|
+
|
|
17
|
+
export { defineBot, definePlatform }
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ResultCode } from '../core/code.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 定义机器人
|
|
5
|
+
* @param callback
|
|
6
|
+
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
const definePlatform = callback => {
|
|
10
|
+
// 判断是否是函数
|
|
11
|
+
if (typeof callback !== 'function') {
|
|
12
|
+
logger.error({
|
|
13
|
+
code: ResultCode.FailParams,
|
|
14
|
+
message: 'Invalid callback: callback must be a function',
|
|
15
|
+
data: null
|
|
16
|
+
})
|
|
17
|
+
throw new Error('Invalid callback: callback must be a function')
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
_name: 'platform',
|
|
21
|
+
callback
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
global.definePlatform = definePlatform
|
|
25
|
+
/**
|
|
26
|
+
* 废弃,请使用 definePlatform
|
|
27
|
+
* @deprecated
|
|
28
|
+
*/
|
|
29
|
+
const defineBot = callback => {
|
|
30
|
+
logger.warn({
|
|
31
|
+
code: ResultCode.Warn,
|
|
32
|
+
message: 'defineBot is deprecated, please use definePlatform',
|
|
33
|
+
data: null
|
|
34
|
+
})
|
|
35
|
+
return definePlatform(callback)
|
|
36
|
+
}
|
|
37
|
+
global.defineBot = defineBot
|
|
38
|
+
|
|
39
|
+
export { defineBot, definePlatform }
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isAsyncFunction } from 'util/types';
|
|
2
2
|
import { useState } from './hook-use-state.js';
|
|
3
|
-
import { showErrorModule } from '
|
|
3
|
+
import { showErrorModule } from '../core/utils.js';
|
|
4
4
|
import { Response } from './store.js';
|
|
5
5
|
import { useMessage } from './hook-use-api.js';
|
|
6
6
|
import { EventMessageText } from '../core/variable.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isAsyncFunction } from 'util/types';
|
|
2
2
|
import { useState } from './hook-use-state.js';
|
|
3
|
-
import { showErrorModule } from '
|
|
3
|
+
import { showErrorModule } from '../core/utils.js';
|
|
4
4
|
import { Middleware } from './store.js';
|
|
5
5
|
import { useMessage } from './hook-use-api.js';
|
|
6
6
|
import { EventMessageText } from '../core/variable.js';
|
|
@@ -2,7 +2,7 @@ import { getConfigValue } from '../core/config.js';
|
|
|
2
2
|
import { processor_repeated_clear_time_min, processor_repeated_clear_time_max, processor_repeated_event_time, processor_repeated_user_time, processor_repeated_clear_size } from '../core/variable.js';
|
|
3
3
|
import { expendCycle } from './event-processor-cycle.js';
|
|
4
4
|
import { ProcessorEventAutoClearMap, ProcessorEventUserAudoClearMap } from './store.js';
|
|
5
|
-
import { createHash } from '
|
|
5
|
+
import { createHash } from '../core/utils.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* 过滤掉重复消息
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EventKeys, Events } from '../typing/event/map.js';
|
|
2
|
-
import { Result } from '
|
|
2
|
+
import { Result } from '../core/utils.js';
|
|
3
3
|
import '../global.js';
|
|
4
4
|
import { DataEnums } from '../typing/message/index.js';
|
|
5
5
|
|
|
@@ -91,5 +91,12 @@ declare const onSelects: <T extends EventKeys[] | EventKeys>(values: T) => T;
|
|
|
91
91
|
* @deprecated
|
|
92
92
|
*/
|
|
93
93
|
declare const createSelects: <T extends EventKeys[] | EventKeys>(values: T) => T;
|
|
94
|
+
/**
|
|
95
|
+
* 使用客户端
|
|
96
|
+
* @param event
|
|
97
|
+
* @param _ApiClass
|
|
98
|
+
* @returns
|
|
99
|
+
*/
|
|
100
|
+
declare const useClient: <T extends object>(event: any, _ApiClass: new (...args: any[]) => T) => T[];
|
|
94
101
|
|
|
95
|
-
export { createSelects, onSelects, unChildren, useChannel, useMenber, useMention, useMessage, useSend, useSends };
|
|
102
|
+
export { createSelects, onSelects, unChildren, useChannel, useClient, useMenber, useMention, useMessage, useSend, useSends };
|
package/lib/app/hook-use-api.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { ResultCode } from '../core/code.js';
|
|
2
2
|
import { ChildrenApp } from './store.js';
|
|
3
|
-
import { createResult } from '
|
|
3
|
+
import { createResult } from '../core/utils.js';
|
|
4
4
|
import { sendAction } from '../cbp/actions.js';
|
|
5
|
+
import { sendAPI } from '../cbp/api.js';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* 使用提及。
|
|
@@ -339,5 +340,28 @@ global.onSelects = onSelects;
|
|
|
339
340
|
* @deprecated
|
|
340
341
|
*/
|
|
341
342
|
const createSelects = onSelects;
|
|
343
|
+
/**
|
|
344
|
+
* 使用客户端
|
|
345
|
+
* @param event
|
|
346
|
+
* @param _ApiClass
|
|
347
|
+
* @returns
|
|
348
|
+
*/
|
|
349
|
+
const useClient = (event, _ApiClass) => {
|
|
350
|
+
const client = new Proxy({}, {
|
|
351
|
+
get(_target, prop) {
|
|
352
|
+
return (...args) => {
|
|
353
|
+
return sendAPI({
|
|
354
|
+
action: 'client.api',
|
|
355
|
+
payload: {
|
|
356
|
+
event,
|
|
357
|
+
key: String(prop),
|
|
358
|
+
params: args
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
return [client];
|
|
365
|
+
};
|
|
342
366
|
|
|
343
|
-
export { createSelects, onSelects, unChildren, useChannel, useMenber, useMention, useMessage, useSend, useSends };
|
|
367
|
+
export { createSelects, onSelects, unChildren, useChannel, useClient, useMenber, useMention, useMessage, useSend, useSends };
|
package/lib/app/load.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { dirname, join } from 'path';
|
|
2
2
|
import { existsSync } from 'fs';
|
|
3
|
-
import { showErrorModule, getRecursiveDirFiles, createEventName } from '
|
|
3
|
+
import { showErrorModule, getRecursiveDirFiles, createEventName } from '../core/utils.js';
|
|
4
4
|
import { createRequire } from 'module';
|
|
5
5
|
import { ChildrenApp } from './store.js';
|
|
6
6
|
import { ResultCode } from '../core/code.js';
|
package/lib/app/message-api.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Result } from './utils.js';
|
|
2
1
|
import { OnDataFormatFunc } from '../typing/event/index.js';
|
|
3
|
-
import '../
|
|
2
|
+
import { Result } from '../core/utils.js';
|
|
4
3
|
import { DataEnums } from '../typing/message/index.js';
|
|
4
|
+
import '../global.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* 创建数据格式。
|
package/lib/cbp/actions.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ResultCode } from '../core/code.js';
|
|
2
|
-
import { createResult } from '../
|
|
2
|
+
import { createResult } from '../core/utils.js';
|
|
3
3
|
import { generateUniqueId, actionResolves, deviceId, actionTimeouts, timeoutTime } from './config.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -11,20 +11,23 @@ const sendAction = (data) => {
|
|
|
11
11
|
return new Promise(resolve => {
|
|
12
12
|
actionResolves.set(actionId, resolve);
|
|
13
13
|
// 设置唯一标识符
|
|
14
|
-
data.
|
|
14
|
+
data.actionId = actionId;
|
|
15
15
|
// 设置设备 ID
|
|
16
16
|
data.DeviceId = deviceId;
|
|
17
|
+
// 发送消息
|
|
17
18
|
global.chatbotClient.send(JSON.stringify(data));
|
|
18
19
|
// 12 秒后超时
|
|
19
20
|
const timeout = setTimeout(() => {
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
// 被清理了
|
|
22
|
+
if (!actionResolves.has(actionId) || !actionTimeouts.has(actionId)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
24
25
|
// 删除回调
|
|
25
26
|
actionResolves.delete(actionId);
|
|
26
27
|
// 删除超时器
|
|
27
28
|
actionTimeouts.delete(actionId);
|
|
29
|
+
// 不会当错误进行处理。而是传入错误码
|
|
30
|
+
resolve([createResult(ResultCode.Fail, '行为超时', null)]);
|
|
28
31
|
}, timeoutTime);
|
|
29
32
|
actionTimeouts.set(actionId, timeout);
|
|
30
33
|
});
|
package/lib/cbp/api.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ResultCode } from '../core/code.js';
|
|
2
|
+
import { createResult } from '../core/utils.js';
|
|
3
|
+
import { generateUniqueId, apiResolves, deviceId, apiTimeouts, timeoutTime } from './config.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 发送行为
|
|
7
|
+
* @param data
|
|
8
|
+
*/
|
|
9
|
+
const sendAPI = (data) => {
|
|
10
|
+
const ApiId = generateUniqueId();
|
|
11
|
+
return new Promise(resolve => {
|
|
12
|
+
apiResolves.set(ApiId, resolve);
|
|
13
|
+
// 设置唯一标识符
|
|
14
|
+
data.apiId = ApiId;
|
|
15
|
+
// 设置设备 ID
|
|
16
|
+
data.DeviceId = deviceId;
|
|
17
|
+
// 发送消息
|
|
18
|
+
global.chatbotClient.send(JSON.stringify(data));
|
|
19
|
+
// 12 秒后超时
|
|
20
|
+
const timeout = setTimeout(() => {
|
|
21
|
+
// 被清理了
|
|
22
|
+
if (!apiResolves.has(ApiId) || !apiTimeouts.has(ApiId)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
// 删除回调
|
|
26
|
+
apiResolves.delete(ApiId);
|
|
27
|
+
// 删除超时器
|
|
28
|
+
apiTimeouts.delete(ApiId);
|
|
29
|
+
// 不会当错误进行处理。而是传入错误码
|
|
30
|
+
resolve([createResult(ResultCode.Fail, '接口超时', null)]);
|
|
31
|
+
}, timeoutTime);
|
|
32
|
+
apiTimeouts.set(ApiId, timeout);
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export { sendAPI };
|
package/lib/cbp/config.js
CHANGED
|
@@ -15,8 +15,12 @@ const DEVICE_ID_HEADER = 'x-device-id';
|
|
|
15
15
|
const FULL_RECEIVE_HEADER = 'x-full-receive';
|
|
16
16
|
// 行为回调
|
|
17
17
|
const actionResolves = new Map();
|
|
18
|
+
// 接口回调
|
|
19
|
+
const apiResolves = new Map();
|
|
18
20
|
// 超时器
|
|
19
21
|
const actionTimeouts = new Map();
|
|
22
|
+
// 接口超时器
|
|
23
|
+
const apiTimeouts = new Map();
|
|
20
24
|
// 分配绑定记录
|
|
21
25
|
const childrenBind = new Map();
|
|
22
26
|
// 生成唯一标识符
|
|
@@ -30,4 +34,4 @@ const reconnectInterval = 1000 * 6; // 6秒
|
|
|
30
34
|
// 心跳间隔
|
|
31
35
|
const HEARTBEAT_INTERVAL = 1000 * 18; // 18秒
|
|
32
36
|
|
|
33
|
-
export { DEVICE_ID_HEADER, FULL_RECEIVE_HEADER, HEARTBEAT_INTERVAL, USER_AGENT_HEADER, actionResolves, actionTimeouts, childrenBind, childrenClient, deviceId, fullClient, generateUniqueId, platformClient, reconnectInterval, timeoutTime };
|
|
37
|
+
export { DEVICE_ID_HEADER, FULL_RECEIVE_HEADER, HEARTBEAT_INTERVAL, USER_AGENT_HEADER, actionResolves, actionTimeouts, apiResolves, apiTimeouts, childrenBind, childrenClient, deviceId, fullClient, generateUniqueId, platformClient, reconnectInterval, timeoutTime };
|
package/lib/cbp/connect.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Actions } from '../typing/actions.js';
|
|
2
2
|
import { EventsEnum } from '../typing/event/map.js';
|
|
3
|
-
import { Result } from '../
|
|
3
|
+
import { Result } from '../core/utils.js';
|
|
4
4
|
import '../global.js';
|
|
5
|
+
import { Apis } from '../typing/apis.js';
|
|
5
6
|
|
|
6
7
|
type CBPClientOptions = {
|
|
7
8
|
open?: () => void;
|
|
@@ -13,12 +14,14 @@ type CBPClientOptions = {
|
|
|
13
14
|
* @param onopen
|
|
14
15
|
*/
|
|
15
16
|
declare const cbpClient: (url: string, options?: CBPClientOptions) => void;
|
|
16
|
-
type
|
|
17
|
+
type ActionReplyFunc = (data: Actions, consume: (payload: Result[]) => void) => void;
|
|
18
|
+
type ApiReplyFunc = (data: Apis, consume: (payload: Result[]) => void) => void;
|
|
17
19
|
declare const cbpPlatform: (url: string, options?: {
|
|
18
20
|
open: () => void;
|
|
19
21
|
}) => {
|
|
20
22
|
send: (data: EventsEnum) => void;
|
|
21
|
-
onactions: (reply:
|
|
23
|
+
onactions: (reply: ActionReplyFunc) => void;
|
|
24
|
+
onapis: (reply: ApiReplyFunc) => void;
|
|
22
25
|
};
|
|
23
26
|
|
|
24
27
|
export { cbpClient, cbpPlatform };
|
package/lib/cbp/connect.js
CHANGED
|
@@ -1,24 +1,11 @@
|
|
|
1
1
|
import { WebSocket } from 'ws';
|
|
2
2
|
import { onProcessor } from '../app/event-processor.js';
|
|
3
3
|
import { ResultCode } from '../core/code.js';
|
|
4
|
-
import { deviceId, FULL_RECEIVE_HEADER, DEVICE_ID_HEADER, USER_AGENT_HEADER, actionResolves, actionTimeouts, reconnectInterval, HEARTBEAT_INTERVAL } from './config.js';
|
|
5
|
-
import '../
|
|
6
|
-
import '../app/event-middleware.js';
|
|
7
|
-
import '../app/event-response.js';
|
|
8
|
-
import '../app/hook-use-api.js';
|
|
9
|
-
import '../typing/event/actions.js';
|
|
10
|
-
import 'fs';
|
|
11
|
-
import 'path';
|
|
12
|
-
import 'yaml';
|
|
13
|
-
import 'node:fs';
|
|
14
|
-
import 'log4js';
|
|
15
|
-
import '../app/load.js';
|
|
16
|
-
import '../app/message-api.js';
|
|
17
|
-
import '../app/message-format.js';
|
|
18
|
-
import { createResult } from '../app/utils.js';
|
|
4
|
+
import { deviceId, FULL_RECEIVE_HEADER, DEVICE_ID_HEADER, USER_AGENT_HEADER, apiResolves, apiTimeouts, actionResolves, actionTimeouts, reconnectInterval, HEARTBEAT_INTERVAL } from './config.js';
|
|
5
|
+
import { createResult } from '../core/utils.js';
|
|
19
6
|
|
|
20
7
|
// 心跳
|
|
21
|
-
const useHeartbeat = ({ ping, isConnected, terminate
|
|
8
|
+
const useHeartbeat = ({ ping, isConnected, terminate }) => {
|
|
22
9
|
let heartbeatTimer = null;
|
|
23
10
|
let lastPong = Date.now();
|
|
24
11
|
const stopHeartbeat = () => {
|
|
@@ -145,15 +132,37 @@ const cbpClient = (url, options = {}) => {
|
|
|
145
132
|
}
|
|
146
133
|
}
|
|
147
134
|
}
|
|
148
|
-
else if (parsedMessage?.
|
|
149
|
-
// 如果有
|
|
150
|
-
const resolve =
|
|
135
|
+
else if (parsedMessage?.apiId) {
|
|
136
|
+
// 如果有 apiId,说明是一个接口请求。要进行处理
|
|
137
|
+
const resolve = apiResolves.get(parsedMessage.apiId);
|
|
151
138
|
if (resolve) {
|
|
139
|
+
apiResolves.delete(parsedMessage.apiId);
|
|
152
140
|
// 清除超时器
|
|
153
|
-
const timeout =
|
|
141
|
+
const timeout = apiTimeouts.get(parsedMessage.apiId);
|
|
154
142
|
if (timeout) {
|
|
143
|
+
apiTimeouts.delete(parsedMessage.apiId);
|
|
144
|
+
clearTimeout(timeout);
|
|
145
|
+
}
|
|
146
|
+
// 调用回调函数
|
|
147
|
+
if (Array.isArray(parsedMessage.payload)) {
|
|
148
|
+
resolve(parsedMessage.payload);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
// 错误处理
|
|
152
|
+
resolve([createResult(ResultCode.Fail, '接口处理错误', null)]);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
else if (parsedMessage?.actionId) {
|
|
157
|
+
// 如果有 actionId
|
|
158
|
+
const resolve = actionResolves.get(parsedMessage.actionId);
|
|
159
|
+
if (resolve) {
|
|
160
|
+
actionResolves.delete(parsedMessage.actionId);
|
|
161
|
+
// 清除超时器
|
|
162
|
+
const timeout = actionTimeouts.get(parsedMessage.actionId);
|
|
163
|
+
if (timeout) {
|
|
164
|
+
actionTimeouts.delete(parsedMessage.actionId);
|
|
155
165
|
clearTimeout(timeout);
|
|
156
|
-
actionTimeouts.delete(parsedMessage.actionID);
|
|
157
166
|
}
|
|
158
167
|
// 调用回调函数
|
|
159
168
|
if (Array.isArray(parsedMessage.payload)) {
|
|
@@ -163,7 +172,6 @@ const cbpClient = (url, options = {}) => {
|
|
|
163
172
|
// 错误处理
|
|
164
173
|
resolve([createResult(ResultCode.Fail, '消费处理错误', null)]);
|
|
165
174
|
}
|
|
166
|
-
actionResolves.delete(parsedMessage.actionID);
|
|
167
175
|
}
|
|
168
176
|
}
|
|
169
177
|
else if (parsedMessage.name) {
|
|
@@ -239,29 +247,47 @@ const cbpPlatform = (url, options = {
|
|
|
239
247
|
global.chatbotPlatform.send(JSON.stringify(data));
|
|
240
248
|
}
|
|
241
249
|
};
|
|
242
|
-
const
|
|
250
|
+
const actionReplys = [];
|
|
251
|
+
const apiReplys = [];
|
|
243
252
|
/**
|
|
244
253
|
* 消费数据
|
|
245
254
|
* @param data
|
|
246
255
|
* @param payload
|
|
247
256
|
*/
|
|
248
|
-
const
|
|
257
|
+
const replyAction = (data, payload) => {
|
|
249
258
|
if (global.chatbotPlatform && global.chatbotPlatform.readyState === WebSocket.OPEN) {
|
|
259
|
+
// 透传消费。也就是对应的设备进行处理消费。
|
|
250
260
|
global.chatbotPlatform.send(JSON.stringify({
|
|
251
261
|
action: data.action,
|
|
252
262
|
payload: payload,
|
|
253
|
-
|
|
254
|
-
// 透传消费。也就是对应的设备进行处理消费。
|
|
263
|
+
actionId: data.actionId,
|
|
255
264
|
DeviceId: data.DeviceId
|
|
256
265
|
}));
|
|
257
266
|
}
|
|
258
267
|
};
|
|
268
|
+
const replyApi = (data, payload) => {
|
|
269
|
+
if (global.chatbotPlatform && global.chatbotPlatform.readyState === WebSocket.OPEN) {
|
|
270
|
+
// 透传消费。也就是对应的设备进行处理消费。
|
|
271
|
+
global.chatbotPlatform.send(JSON.stringify({
|
|
272
|
+
action: data.action,
|
|
273
|
+
apiId: data.apiId,
|
|
274
|
+
DeviceId: data.DeviceId,
|
|
275
|
+
payload: payload
|
|
276
|
+
}));
|
|
277
|
+
}
|
|
278
|
+
};
|
|
259
279
|
/**
|
|
260
280
|
* 接收行为
|
|
261
281
|
* @param reply
|
|
262
282
|
*/
|
|
263
283
|
const onactions = (reply) => {
|
|
264
|
-
|
|
284
|
+
actionReplys.push(reply);
|
|
285
|
+
};
|
|
286
|
+
/**
|
|
287
|
+
* 接收接口
|
|
288
|
+
*/
|
|
289
|
+
const onapis = (reply) => {
|
|
290
|
+
apiReplys.push(reply);
|
|
265
291
|
};
|
|
266
292
|
/**
|
|
267
293
|
* 启动 WebSocket 连接
|
|
@@ -288,10 +314,19 @@ const cbpPlatform = (url, options = {
|
|
|
288
314
|
message: '平台端接收消息',
|
|
289
315
|
data: data
|
|
290
316
|
});
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
317
|
+
if (data.apiId) {
|
|
318
|
+
for (const cb of apiReplys) {
|
|
319
|
+
cb(data,
|
|
320
|
+
// 传入一个消费函数
|
|
321
|
+
val => replyApi(data, val));
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
else if (data.actionId) {
|
|
325
|
+
for (const cb of actionReplys) {
|
|
326
|
+
cb(data,
|
|
327
|
+
// 传入一个消费函数
|
|
328
|
+
val => replyAction(data, val));
|
|
329
|
+
}
|
|
295
330
|
}
|
|
296
331
|
}
|
|
297
332
|
catch (error) {
|
|
@@ -326,7 +361,8 @@ const cbpPlatform = (url, options = {
|
|
|
326
361
|
start();
|
|
327
362
|
const client = {
|
|
328
363
|
send,
|
|
329
|
-
onactions
|
|
364
|
+
onactions,
|
|
365
|
+
onapis
|
|
330
366
|
};
|
|
331
367
|
return client;
|
|
332
368
|
};
|
package/lib/cbp/index.d.ts
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
import { Actions } from '../typing/actions.js'
|
|
2
|
+
import { EventsEnum } from '../typing/event/map.js'
|
|
3
|
+
import '../global.js'
|
|
4
|
+
|
|
1
5
|
declare const cbpServer: (port: number, listeningListener?: () => void) => void
|
|
6
|
+
/**
|
|
7
|
+
* 发送行为
|
|
8
|
+
* @param data
|
|
9
|
+
*/
|
|
10
|
+
declare const sendAction: (data: Actions) => Promise<any>
|
|
11
|
+
type CBPClientOptions = {
|
|
12
|
+
open?: () => void
|
|
13
|
+
isFullReceive?: boolean
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* CBP 客户端
|
|
17
|
+
* @param url
|
|
18
|
+
* @param onopen
|
|
19
|
+
*/
|
|
20
|
+
declare const cbpClient: (url: string, options?: CBPClientOptions) => void
|
|
21
|
+
type ReplyFunc = (data: Actions, consume: (payload: any) => void) => void
|
|
22
|
+
declare const cbpPlatform: (
|
|
23
|
+
url: string,
|
|
24
|
+
options?: {
|
|
25
|
+
open: () => void
|
|
26
|
+
}
|
|
27
|
+
) => {
|
|
28
|
+
send: (data: EventsEnum) => void
|
|
29
|
+
onactions: (reply: ReplyFunc) => void
|
|
30
|
+
}
|
|
2
31
|
|
|
3
|
-
export { cbpServer }
|
|
32
|
+
export { cbpClient, cbpPlatform, cbpServer, sendAction }
|
package/lib/cbp/index.js
CHANGED
|
@@ -47,14 +47,41 @@ const cbpServer = (port, listeningListener) => {
|
|
|
47
47
|
try {
|
|
48
48
|
// 解析消息
|
|
49
49
|
const parsedMessage = JSON.parse(message.toString());
|
|
50
|
-
// 1. 解析得到
|
|
50
|
+
// 1. 解析得到 actionId ,说明是消费行为请求。要广播告诉所有客户端。
|
|
51
51
|
// 2. 解析得到 name ,说明是一个事件请求。
|
|
52
|
+
// 3. 解析得到 apiId ,说明是一个接口请求。
|
|
52
53
|
logger.debug({
|
|
53
54
|
code: ResultCode.Ok,
|
|
54
55
|
message: '服务端接收到消息',
|
|
55
56
|
data: parsedMessage
|
|
56
57
|
});
|
|
57
|
-
if (parsedMessage
|
|
58
|
+
if (!parsedMessage.apiId) {
|
|
59
|
+
// 指定的设备 处理消费。终端有记录每个客户端是谁
|
|
60
|
+
const DeviceId = parsedMessage.DeviceId;
|
|
61
|
+
if (childrenClient.has(DeviceId)) {
|
|
62
|
+
const clientWs = childrenClient.get(DeviceId);
|
|
63
|
+
if (clientWs && clientWs.readyState === WebSocket.OPEN) {
|
|
64
|
+
// 发送消息到指定的子客户端
|
|
65
|
+
clientWs.send(message);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// 如果连接已关闭,删除该客户端
|
|
69
|
+
childrenClient.delete(DeviceId);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else if (fullClient.has(DeviceId)) {
|
|
73
|
+
const clientWs = fullClient.get(DeviceId);
|
|
74
|
+
if (clientWs && clientWs.readyState === WebSocket.OPEN) {
|
|
75
|
+
// 发送消息到指定的全量客户端
|
|
76
|
+
clientWs.send(message);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
// 如果连接已关闭,删除该客户端
|
|
80
|
+
fullClient.delete(DeviceId);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else if (parsedMessage?.actionId) {
|
|
58
85
|
// 指定的设备 处理消费。终端有记录每个客户端是谁
|
|
59
86
|
const DeviceId = parsedMessage.DeviceId;
|
|
60
87
|
if (childrenClient.has(DeviceId)) {
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Dirent } from 'fs';
|
|
2
|
+
import { ResultCode } from './code.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 将字符串转为定长字符串
|
|
6
|
+
* @param str 输入字符串
|
|
7
|
+
* @param options 可选项
|
|
8
|
+
* @returns 固定长度的哈希值
|
|
9
|
+
*/
|
|
10
|
+
declare const createHash: (str: string, options?: {
|
|
11
|
+
length?: number;
|
|
12
|
+
algorithm?: string;
|
|
13
|
+
}) => string;
|
|
14
|
+
/**
|
|
15
|
+
* 使用用户的哈希键
|
|
16
|
+
* @param e
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
declare const useUserHashKey: (event: {
|
|
20
|
+
UserId: string;
|
|
21
|
+
Platform: string;
|
|
22
|
+
}) => string;
|
|
23
|
+
/**
|
|
24
|
+
* 创建app名称
|
|
25
|
+
* @param url
|
|
26
|
+
* @param app 模块名
|
|
27
|
+
* @param select 选择事件类型,默认 res
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
declare const createEventName: (url: string, appKey: string) => string;
|
|
31
|
+
/**
|
|
32
|
+
* 将字符串转为数字
|
|
33
|
+
* @param str
|
|
34
|
+
* @returns
|
|
35
|
+
*/
|
|
36
|
+
declare const stringToNumber: (str: string, size?: number) => number;
|
|
37
|
+
/**
|
|
38
|
+
* 递归获取所有文件
|
|
39
|
+
* @param dir
|
|
40
|
+
* @param condition
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
declare const getRecursiveDirFiles: (dir: string, condition?: (func: Dirent) => boolean) => {
|
|
44
|
+
path: string;
|
|
45
|
+
name: string;
|
|
46
|
+
}[];
|
|
47
|
+
/**
|
|
48
|
+
* 解析错误并提示缺失的模块
|
|
49
|
+
* @param e
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
declare const showErrorModule: (e: Error) => void;
|
|
53
|
+
declare const getInputExportPath: (input?: string) => any;
|
|
54
|
+
/**
|
|
55
|
+
* 属于直接调用的函数
|
|
56
|
+
* 参数错误直接抛出错误
|
|
57
|
+
* 如果函数内部有错误,则使用 Result 进行返回
|
|
58
|
+
*/
|
|
59
|
+
/**
|
|
60
|
+
* 异步方法。且需要读取返回值的进行判断的,
|
|
61
|
+
* 都要以 Result 作为返回值
|
|
62
|
+
*/
|
|
63
|
+
type Result = {
|
|
64
|
+
code: ResultCode;
|
|
65
|
+
message: string | Object;
|
|
66
|
+
data: any;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* 创建结果
|
|
70
|
+
* @param code
|
|
71
|
+
* @param message
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
declare const createResult: (code: ResultCode, message: string | Object, data?: any) => Result;
|
|
75
|
+
|
|
76
|
+
export { createEventName, createHash, createResult, getInputExportPath, getRecursiveDirFiles, showErrorModule, stringToNumber, useUserHashKey };
|
|
77
|
+
export type { Result };
|