mm_socket_server 1.0.1 → 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 +1 -1
- package/index.js +18 -1
- package/lib/message.js +3 -3
- package/lib/socket.js +8 -6
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# mm_socket_server
|
|
2
2
|
|
|
3
|
-
TCP/UDP
|
|
3
|
+
TCP/UDP 套接字服务器入站适配器模块。继承 [mm_corejs](https://gitee.com/qiuwenwu91/mm_corejs) 的 `Mod` 基类,提供连接管理、消息处理管道与 JSON-RPC 2.0 消息插件,可脱离完整框架独立运行。
|
|
4
4
|
|
|
5
5
|
- 入口:`index.js`(CommonJS)
|
|
6
6
|
- 导出:`SocketServer`(服务器模块)、`MessagePlugin`(JSON-RPC 消息插件)
|
package/index.js
CHANGED
|
@@ -2,8 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
const SocketServer = require('./lib/socket');
|
|
4
4
|
const MessagePlugin = require('./lib/message');
|
|
5
|
+
const Admin = require('mm_corejs').Admin;
|
|
6
|
+
|
|
7
|
+
let admin = new Admin();
|
|
8
|
+
admin.setClass(SocketServer);
|
|
5
9
|
|
|
6
10
|
module.exports = {
|
|
7
11
|
SocketServer,
|
|
8
|
-
MessagePlugin
|
|
12
|
+
MessagePlugin,
|
|
13
|
+
admin,
|
|
14
|
+
/**
|
|
15
|
+
* 获取指定作用域的 Socket 服务器
|
|
16
|
+
* @param {string} [scope='sys'] 作用域名称
|
|
17
|
+
* @param {object} [config] 配置参数
|
|
18
|
+
* @returns {SocketServer} Socket 服务器实例
|
|
19
|
+
*/
|
|
20
|
+
socketServerAdmin(scope, config) {
|
|
21
|
+
if (!scope) {
|
|
22
|
+
scope = 'os';
|
|
23
|
+
}
|
|
24
|
+
return admin.get(scope, { name: scope, ...config });
|
|
25
|
+
}
|
|
9
26
|
};
|
package/lib/message.js
CHANGED
|
@@ -50,8 +50,8 @@ class MessagePlugin {
|
|
|
50
50
|
throw new TypeError('[MessagePlugin] server must provide getHandle()');
|
|
51
51
|
}
|
|
52
52
|
this.#server = server;
|
|
53
|
-
server.getHandle().on('main', (
|
|
54
|
-
return this._execute(
|
|
53
|
+
server.getHandle().on('main', (params, ctx, options) => {
|
|
54
|
+
return this._execute(params, ctx, options);
|
|
55
55
|
}, 100, 'json_rpc');
|
|
56
56
|
return this;
|
|
57
57
|
}
|
|
@@ -127,7 +127,7 @@ class MessagePlugin {
|
|
|
127
127
|
* @param {Object} [options] 管道请求选项(连接级,如 user_id / device_id / agent_id),透传给处理函数
|
|
128
128
|
* @returns {Promise<Object|string|undefined>} 响应对象或 JSON 字符串;反馈、透传与无结果通知时为 undefined
|
|
129
129
|
*/
|
|
130
|
-
async _execute(
|
|
130
|
+
async _execute(params, ctx, options) {
|
|
131
131
|
if (this.#rpc.acceptResponse(params.data)) return undefined;
|
|
132
132
|
const extra = { client_id: params.client_id };
|
|
133
133
|
const response = await this.#rpc.handle(params.data, extra, options);
|
package/lib/socket.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
const net = require('net');
|
|
4
4
|
const dgram = require('dgram');
|
|
5
|
-
const { Mod,
|
|
5
|
+
const { Mod, Store } = require('mm_corejs');
|
|
6
|
+
const { Handle } = require('mm_handle');
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* 服务器设置校验 schema(校验对象为顶层 config,而非 run 时选项)
|
|
@@ -19,7 +20,7 @@ const SERVER_SCHEMA = {
|
|
|
19
20
|
};
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
|
-
* SocketServer —— TCP/UDP
|
|
23
|
+
* SocketServer —— TCP/UDP 套接字服务器(入站适配器模块)。
|
|
23
24
|
*
|
|
24
25
|
* 继承 mm_corejs 的 Mod:生命周期由 Lifecycle 模板方法驱动,
|
|
25
26
|
* 调用参数校验由 config.params 声明式定义;
|
|
@@ -46,7 +47,7 @@ class SocketServer extends Mod {
|
|
|
46
47
|
* 模块默认配置
|
|
47
48
|
*/
|
|
48
49
|
static config = {
|
|
49
|
-
name: '
|
|
50
|
+
name: 'os',
|
|
50
51
|
title: 'Socket服务器模块',
|
|
51
52
|
description: 'TCP/UDP 套接字服务器,管理连接并分发消息',
|
|
52
53
|
version: '1.0.0',
|
|
@@ -80,6 +81,7 @@ class SocketServer extends Mod {
|
|
|
80
81
|
|
|
81
82
|
/**
|
|
82
83
|
* 消息处理管道(before/check/main/render/error/success/after)
|
|
84
|
+
* 每个实例独立 Handle,避免多实例共享管道导致钩子泄漏
|
|
83
85
|
*/
|
|
84
86
|
#handle = new Handle();
|
|
85
87
|
|
|
@@ -127,12 +129,12 @@ class SocketServer extends Mod {
|
|
|
127
129
|
// ---------- 执行入口 ----------
|
|
128
130
|
/**
|
|
129
131
|
* 模块主逻辑:按 action 派发操作
|
|
130
|
-
* @param {Object} ctx 执行上下文
|
|
131
132
|
* @param {Object} params 调用参数,含 action 及操作所需字段
|
|
133
|
+
* @param {Object} ctx 执行上下文
|
|
132
134
|
* @param {Object} options 合并后的选项
|
|
133
135
|
* @returns {Promise<Object>} 操作结果
|
|
134
136
|
*/
|
|
135
|
-
async main(
|
|
137
|
+
async main(params, ctx, options) {
|
|
136
138
|
const action = params.action;
|
|
137
139
|
if (action === 'send') return this._actionSend(params);
|
|
138
140
|
if (action === 'broadcast') return this._actionBroadcast(params);
|
|
@@ -532,7 +534,7 @@ class SocketServer extends Mod {
|
|
|
532
534
|
* 不返回值,业务钩子产出的 result 由 _handleData 统一回发)
|
|
533
535
|
*/
|
|
534
536
|
_initHandle() {
|
|
535
|
-
this.#handle.on('main', (
|
|
537
|
+
this.#handle.on('main', (params, ctx) => {
|
|
536
538
|
this._safeEmit('message', params.client_id, params.data);
|
|
537
539
|
}, 1000, 'emit_message');
|
|
538
540
|
}
|
package/package.json
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_socket_server",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "TCP/UDP
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "TCP/UDP 套接字服务器入站适配器模块,继承 mm_corejs 的 Mod 基类,支持独立运行。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"超级美眉",
|
|
7
|
-
"
|
|
7
|
+
"入站适配器",
|
|
8
8
|
"mm_mod",
|
|
9
|
-
"
|
|
9
|
+
"mm_adapter",
|
|
10
|
+
"mm_in_adapter",
|
|
10
11
|
"mm_receiver",
|
|
11
12
|
"socket",
|
|
12
13
|
"tcp",
|
|
13
14
|
"udp",
|
|
14
15
|
"server"
|
|
15
16
|
],
|
|
17
|
+
"layer": "adapter",
|
|
18
|
+
"group": "in_adapter",
|
|
16
19
|
"license": "ISC",
|
|
17
20
|
"author": "qww",
|
|
18
21
|
"type": "commonjs",
|
|
@@ -38,7 +41,7 @@
|
|
|
38
41
|
"test": "jest --coverage"
|
|
39
42
|
},
|
|
40
43
|
"dependencies": {
|
|
41
|
-
"mm_corejs": "^1.1.
|
|
44
|
+
"mm_corejs": "^1.1.4"
|
|
42
45
|
},
|
|
43
46
|
"devDependencies": {
|
|
44
47
|
"jest": "^30.5.2"
|