ming_node 2.2.2 → 2.2.7
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +11 -1
- package/beforeTest/ApiCloudBaseRpcApiTest.js +17 -0
- package/beforeTest/FileBaseRpcApiTest.js +8 -0
- package/beforeTest/MemorDbTest.js +16 -0
- package/beforeTest/MemoryBaseRestApiTest.js +8 -0
- package/beforeTest/MemoryBaseRpcApiTest.js +8 -0
- package/beforeTest/MiApiCloudClientTest.js +16 -0
- package/beforeTest/MongoDbBaseRestApiTest.js +22 -0
- package/beforeTest/MongoDbBaseRpcApiTest.js +19 -0
- package/beforeTest/MySqlBaseRestApiTest.js +13 -0
- package/beforeTest/MysqlBaseRpcApiTest.js +18 -0
- package/beforeTest/graphql_test.js +12 -0
- package/beforeTest/installPluginTest.js +10 -0
- package/beforeTest/sseserver.js +44 -0
- package/beforeTest/static/ssetest.html +21 -0
- package/index.js +198 -44
- package/ming_node.md +4 -4
- package/module/BaseMapper.js +90 -35
- package/module/MemoryDb.js +136 -0
- package/module/MiApiCloudClient.js +649 -0
- package/package.json +1 -1
- package/plugins/BaseGraphqlApi/getGraphqlSchema.js +145 -0
- package/plugins/BaseGraphqlApi/getGraphqlSchemaDemo.js +76 -0
- package/plugins/BaseGraphqlApi/index.js +23 -0
- package/plugins/BaseRestApi/AbstractBaseRestApi.js +59 -0
- package/plugins/BaseRestApi/ApiCloudBaseRestApi.js +56 -0
- package/plugins/BaseRestApi/FileBaseRestApi.js +62 -0
- package/plugins/BaseRestApi/MemoryBaseRestApi.js +50 -0
- package/plugins/BaseRestApi/MongoDbBaseRestApi.js +75 -0
- package/plugins/BaseRestApi/MysqlBaseRestApi.js +72 -0
- package/plugins/BaseRpcApi/AbstractBaseRpcApi.js +72 -0
- package/plugins/BaseRpcApi/ApiCloudBaseRpcApi.js +57 -0
- package/plugins/BaseRpcApi/FileBaseRpcApi.js +62 -0
- package/plugins/BaseRpcApi/MemoryBaseRpcApi.js +50 -0
- package/plugins/BaseRpcApi/MongoDbBaseRpcApi.js +75 -0
- package/plugins/BaseRpcApi/MysqlBaseRpcApi.js +72 -0
- package/utils/common/CollectionUtils.js +28 -0
- package/beforeTest/t1.js +0 -18
package/README.md
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
```
|
11
11
|
# cdn
|
12
12
|
|
13
|
-
https://unpkg.com/ming_node
|
13
|
+
https://unpkg.com/ming_node/index.js
|
14
14
|
|
15
15
|
|
16
16
|
# ming_node最小环境
|
@@ -190,6 +190,16 @@ async function main(){
|
|
190
190
|
|
191
191
|
```
|
192
192
|
|
193
|
+
# Rpc风格,服务插件
|
194
|
+
[ming_node api插件.yuque](https://www.yuque.com/docs/share/f4444345-ea5b-4f3d-b0c7-ab267e901e81)
|
195
|
+
```js
|
196
|
+
var M=require("ming_node");
|
197
|
+
const Api= require("ming_node/plugins/BaseRpcApi/MemoryBaseRpcApi");
|
198
|
+
let api = new Api({tableName:"ming",generateTime:true})
|
199
|
+
var app=M.server();
|
200
|
+
app.listen(8888);
|
201
|
+
app.use(api);
|
202
|
+
```
|
193
203
|
|
194
204
|
# 基于ming_node 的 ming_api_mock
|
195
205
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
const ApiCloudBaseRpcApi= require("../plugins/BaseRpcApi/ApiCloudBaseRpcApi");
|
3
|
+
const MiApiCloudClient=require("../module/MiApiCloudClient");
|
4
|
+
const apiCloudClient = new MiApiCloudClient("A6032931027980", "FF279F8E-8B09-5F1A-1036-F6AE53F3538D");
|
5
|
+
let apiCloudBaseRpcApi = new ApiCloudBaseRpcApi(
|
6
|
+
{
|
7
|
+
prefix:"ming",
|
8
|
+
tableName:"ming",
|
9
|
+
apiCloudClient
|
10
|
+
}
|
11
|
+
)
|
12
|
+
|
13
|
+
var app=M.server();
|
14
|
+
|
15
|
+
app.listen(8888);
|
16
|
+
|
17
|
+
app.use(apiCloudBaseRpcApi);
|
@@ -0,0 +1,8 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
const FileBaseRpcApi= require("../plugins/BaseRpcApi/FileBaseRpcApi");
|
3
|
+
|
4
|
+
let fileBaseRpcApi = new FileBaseRpcApi({prefix:"ming", tableName:"a.json",generateTime:true})
|
5
|
+
var app=M.server();
|
6
|
+
app.listen(8888);
|
7
|
+
|
8
|
+
app.use(fileBaseRpcApi);
|
@@ -0,0 +1,8 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
const MemoryBaseRestApi= require("../plugins/BaseRestApi/MemoryBaseRestApi");
|
3
|
+
|
4
|
+
let memoryBaseRestApi = new MemoryBaseRestApi({tableName:"ming",generateTime:true})
|
5
|
+
var app=M.server();
|
6
|
+
app.listen(8888);
|
7
|
+
|
8
|
+
app.use(memoryBaseRestApi);
|
@@ -0,0 +1,16 @@
|
|
1
|
+
const MiApiCloudClient=require("../module/MiApiCloudClient")
|
2
|
+
|
3
|
+
|
4
|
+
M.MiApiCloudClient = new MiApiCloudClient("A6032931027980", "FF279F8E-8B09-5F1A-1036-F6AE53F3538D").tableClient("mi_user");
|
5
|
+
|
6
|
+
|
7
|
+
mi_resource.list({},null,null,"sort").then(d => {
|
8
|
+
let result = { rows: d }
|
9
|
+
console.log(result)
|
10
|
+
})
|
11
|
+
|
12
|
+
// mi_resource.add({
|
13
|
+
// username:"minglie2234"
|
14
|
+
// }).then(d=>{
|
15
|
+
// console.log(d)
|
16
|
+
// })
|
@@ -0,0 +1,22 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
|
3
|
+
|
4
|
+
Db= M.getMongoDB({
|
5
|
+
dbUrl: "mongodb://root:123456@localhost:27017/?authMechanism=SCRAM-SHA-1&authSource=miapi",
|
6
|
+
dbName:"miapi"
|
7
|
+
})
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
const MongoDbBaseRestApi= require("../plugins/BaseRestApi/MongoDbBaseRestApi");
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
let mongoDbBaseRestApi = new MongoDbBaseRestApi({tableName:"test",prefix:"ming", generateTime:true})
|
16
|
+
var app=M.server();
|
17
|
+
app.listen(8888);
|
18
|
+
|
19
|
+
app.use(mongoDbBaseRestApi);
|
20
|
+
|
21
|
+
|
22
|
+
console.log(M._globle_plugin)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
|
3
|
+
|
4
|
+
Db= M.getMongoDB({
|
5
|
+
dbUrl: "mongodb://root:123456@localhost:27017/?authMechanism=SCRAM-SHA-1&authSource=miapi",
|
6
|
+
dbName:"miapi"
|
7
|
+
})
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
const MongoDbBaseRpcApi= require("../plugins/BaseRpcApi/MongoDbBaseRpcApi");
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
let mongoDbBaseRpcApi = new MongoDbBaseRpcApi({tableName:"test",prefix:"ming", generateTime:true})
|
16
|
+
var app=M.server();
|
17
|
+
app.listen(8888);
|
18
|
+
|
19
|
+
app.use(mongoDbBaseRpcApi);
|
@@ -0,0 +1,13 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
const MysqlBaseRestApi= require("../plugins/BaseRestApi/MysqlBaseRestApi");
|
3
|
+
|
4
|
+
M.getMySql({
|
5
|
+
database:"miapi"
|
6
|
+
})
|
7
|
+
|
8
|
+
|
9
|
+
let mysqlBaseRestApi = new MysqlBaseRestApi({tableName:"ming",generateTime:true})
|
10
|
+
var app=M.server();
|
11
|
+
app.listen(8888);
|
12
|
+
|
13
|
+
app.use(mysqlBaseRestApi);
|
@@ -0,0 +1,18 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
|
3
|
+
|
4
|
+
M.getMySql({
|
5
|
+
database:"miapi"
|
6
|
+
})
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
const MysqlBaseRpcApi= require("../plugins/BaseRpcApi/MysqlBaseRpcApi");
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
let mysqlBaseRpcApi = new MysqlBaseRpcApi({tableName:"ming",generateTime:true})
|
15
|
+
var app=M.server();
|
16
|
+
app.listen(8888);
|
17
|
+
|
18
|
+
app.use(mysqlBaseRpcApi);
|
@@ -0,0 +1,12 @@
|
|
1
|
+
var M = require('../index');
|
2
|
+
var BaseMapper=require("../module/BaseMapper")
|
3
|
+
var Db=M.getMySql({
|
4
|
+
"database" : "miapi"
|
5
|
+
})
|
6
|
+
let dbBaseMapper= new BaseMapper("person");
|
7
|
+
const BaseGraphqlApi= require("../plugins/BaseGraphqlApi/index");
|
8
|
+
let baseGraphqlApi = new BaseGraphqlApi({prefix:"person", dbBaseMapper});
|
9
|
+
|
10
|
+
var app = M.server()
|
11
|
+
app.listen(4000)
|
12
|
+
app.use(baseGraphqlApi);
|
@@ -0,0 +1,44 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
|
3
|
+
var app=M.server();
|
4
|
+
app.listen(8888);
|
5
|
+
|
6
|
+
//SSE 心跳周期
|
7
|
+
M._sseHeatTime=3000;
|
8
|
+
|
9
|
+
|
10
|
+
sseApp=M.sseServer()
|
11
|
+
//sseApp.listen(2000)
|
12
|
+
app.get("/sseServer",sseApp)
|
13
|
+
|
14
|
+
//sse 连接成功钩子
|
15
|
+
app.set("sse_connection",(clientId)=>{
|
16
|
+
console.log(clientId+"连接成功")
|
17
|
+
//返回false 说明不让连接
|
18
|
+
return true;
|
19
|
+
})
|
20
|
+
|
21
|
+
//sse 断开钩子
|
22
|
+
app.set("sse_disconnect",(clientId)=>{
|
23
|
+
console.log(clientId+"断开连接")
|
24
|
+
})
|
25
|
+
|
26
|
+
//查询所有sse客户端
|
27
|
+
app.get("allSseClient",(clientId)=>{
|
28
|
+
res.send(M._sseClientMap.keys());
|
29
|
+
})
|
30
|
+
|
31
|
+
//广播方式发送
|
32
|
+
app.get("/sseSend1",(req,res)=>{
|
33
|
+
console.log(req.params);
|
34
|
+
sseApp.send(req.params);
|
35
|
+
res.send("ok");
|
36
|
+
})
|
37
|
+
|
38
|
+
|
39
|
+
//私信发送
|
40
|
+
app.get("/sseSend2",(req,res)=>{
|
41
|
+
console.log(req.params);
|
42
|
+
sseApp.send(req.params,"clientId");
|
43
|
+
res.send("ok");
|
44
|
+
})
|
@@ -0,0 +1,21 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta charset="utf-8">
|
5
|
+
<title>xxx</title>
|
6
|
+
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
|
7
|
+
<script src="https://minglie.github.io/js/M_mock.js"></script>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
<h1>获取服务端更新数据</h1>
|
11
|
+
<div id="result"></div>
|
12
|
+
<script>
|
13
|
+
|
14
|
+
M.EventSource('http://localhost:8888/sseServer?clientId='+M.randomStr(),function(e){
|
15
|
+
console.log(e.data)
|
16
|
+
})
|
17
|
+
|
18
|
+
</script>
|
19
|
+
|
20
|
+
</body>
|
21
|
+
</html>
|
package/index.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
/**
|
2
|
-
* File :
|
2
|
+
* File : MemoryBaseRpcApi.js
|
3
3
|
* By : Minglie
|
4
4
|
* QQ: 934031452
|
5
|
-
* Date :2021.
|
6
|
-
* version :2.2.
|
5
|
+
* Date :2021.12.01
|
6
|
+
* version :2.2.7
|
7
7
|
*/
|
8
8
|
var http = require('http');
|
9
9
|
var https = require('https');
|
@@ -30,6 +30,9 @@
|
|
30
30
|
M.database_path = "./M_database.json";//文件型数据库路径
|
31
31
|
M.log_display_time = true;//日志是否显示当前时间
|
32
32
|
M.httpProxy = {};// http 代理配置
|
33
|
+
M._sseClientMap=new Map();
|
34
|
+
M._sseHeatTime=3000;
|
35
|
+
M._moduleMap=new Map();//模块map
|
33
36
|
M.httpBefore = (d) => {
|
34
37
|
return d
|
35
38
|
}
|
@@ -39,13 +42,26 @@
|
|
39
42
|
M._globle_cacheMap = {}
|
40
43
|
//全局对象缓存
|
41
44
|
M._globle_lib_cacheMap={}
|
45
|
+
//全局插件地址缓存
|
46
|
+
M._globle_plugin_url_cacheMap={};
|
47
|
+
//全局插件
|
48
|
+
M._globle_plugin=new Set();
|
42
49
|
M._node_lib_path=process.env.NODE_PATH;
|
43
50
|
//远程静态资源路径
|
44
51
|
M.remoteStaticPath = "https://minglie.gitee.io/mingpage/static";
|
45
52
|
M.remoteStaticPathEnable = true;
|
46
53
|
//代理服务器配置
|
47
54
|
M.proxyHost = "http://127.0.0.1:8888"
|
48
|
-
M.proxyHost = ""
|
55
|
+
M.proxyHost = "";
|
56
|
+
|
57
|
+
M.setModule=function (key,module){
|
58
|
+
M._moduleMap.set(key,module);
|
59
|
+
}
|
60
|
+
M.getModule=function (key){
|
61
|
+
M._moduleMap.get(key);
|
62
|
+
}
|
63
|
+
|
64
|
+
|
49
65
|
/**
|
50
66
|
* ----------------------客户端START--------------------------------------------
|
51
67
|
*/
|
@@ -655,13 +671,14 @@ M.request.put=M.put;
|
|
655
671
|
}
|
656
672
|
}
|
657
673
|
M.writeObjToFile(file, d);
|
674
|
+
return d_num;
|
658
675
|
}
|
659
676
|
|
660
677
|
M.updateObjByIdFile = function (file, obj) {
|
661
678
|
var d = M.getObjByFile(file);
|
662
679
|
for (let i = 0; i < d.length; i++) {
|
663
680
|
if (d[i].id == obj.id) {
|
664
|
-
d.splice(i, 1, obj);
|
681
|
+
d.splice(i, 1, Object.assign(d[i],obj));
|
665
682
|
break;
|
666
683
|
}
|
667
684
|
}
|
@@ -675,17 +692,28 @@ M.request.put=M.put;
|
|
675
692
|
}
|
676
693
|
}
|
677
694
|
}
|
678
|
-
M.listAllObjByPropFile = function (file,
|
679
|
-
|
680
|
-
let
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
695
|
+
M.listAllObjByPropFile = function (file, caseObj) {
|
696
|
+
var d = M.getObjByFile(file);
|
697
|
+
let o_keys = Object.keys(caseObj);
|
698
|
+
if (caseObj && o_keys.length>0) {
|
699
|
+
let r_list = [];
|
700
|
+
let o_vals = Object.values(caseObj);
|
701
|
+
for (let i = 0; i < d.length; i++) {
|
702
|
+
let s=0;
|
703
|
+
for (let j=0;j<o_keys.length;j++){
|
704
|
+
if (d[i][o_keys[j]] != o_vals[j]) {
|
705
|
+
break
|
706
|
+
}
|
707
|
+
s++;
|
708
|
+
}
|
709
|
+
if(s==o_keys.length){
|
710
|
+
r_list.push(d[i]);
|
711
|
+
}
|
686
712
|
}
|
713
|
+
return r_list;
|
714
|
+
} else {
|
715
|
+
return d;
|
687
716
|
}
|
688
|
-
return r_list;
|
689
717
|
}
|
690
718
|
/**
|
691
719
|
* 文件型数据库第二层封装
|
@@ -974,6 +1002,7 @@ M.getMySql = function (dbConfig) {
|
|
974
1002
|
timezone: "08:00"
|
975
1003
|
}
|
976
1004
|
var Db = {};
|
1005
|
+
Db.dbConfig=defaultDbConfig;
|
977
1006
|
console.log("connect mysql", defaultDbConfig)
|
978
1007
|
var pool = mysql.createPool(defaultDbConfig);
|
979
1008
|
Db.pool=pool;
|
@@ -1207,8 +1236,9 @@ M.getMongoDB = function (dbConfig) {
|
|
1207
1236
|
}
|
1208
1237
|
|
1209
1238
|
}
|
1210
|
-
|
1211
|
-
|
1239
|
+
MingMongoClient.ObjectID=(id)=> new ObjectID(id)
|
1240
|
+
let Db=MingMongoClient;
|
1241
|
+
Db.dbConfig=Config;
|
1212
1242
|
MingMongoClient.collectionName="test"
|
1213
1243
|
M.mongoDb=Db;
|
1214
1244
|
return Db;
|
@@ -1356,6 +1386,40 @@ M.failResult=(msg,code,d)=>{
|
|
1356
1386
|
M.randomStr = function () {
|
1357
1387
|
return (Math.random().toString(36) + new Date().getTime()).slice(2);
|
1358
1388
|
}
|
1389
|
+
|
1390
|
+
M.urlStringify = function (obj) {
|
1391
|
+
if (obj !== null && typeof obj === 'object') {
|
1392
|
+
var keys = Object.keys(obj);
|
1393
|
+
var len = keys.length;
|
1394
|
+
var flast = len - 1;
|
1395
|
+
var fields = '';
|
1396
|
+
for (var i = 0; i < len; ++i) {
|
1397
|
+
var k = keys[i];
|
1398
|
+
var v = obj[k];
|
1399
|
+
var ks = k + "=";
|
1400
|
+
fields += ks + v;
|
1401
|
+
if (i < flast)
|
1402
|
+
fields += "&";
|
1403
|
+
}
|
1404
|
+
return fields;
|
1405
|
+
}
|
1406
|
+
return '';
|
1407
|
+
};
|
1408
|
+
|
1409
|
+
M.urlParse = function (url) {
|
1410
|
+
url = url.substr(url.indexOf("?") + 1);
|
1411
|
+
var t, n, r, i = url, s = {};
|
1412
|
+
t = i.split("&"),
|
1413
|
+
r = null,
|
1414
|
+
n = null;
|
1415
|
+
for (var o in t) {
|
1416
|
+
var u = t[o].indexOf("=");
|
1417
|
+
u !== -1 && (r = t[o].substr(0, u),
|
1418
|
+
n = t[o].substr(u + 1),
|
1419
|
+
s[r] = n)
|
1420
|
+
}
|
1421
|
+
return s
|
1422
|
+
};
|
1359
1423
|
|
1360
1424
|
/**
|
1361
1425
|
* 异常处理钩子
|
@@ -1661,16 +1725,43 @@ M.failResult=(msg,code,d)=>{
|
|
1661
1725
|
G._server = callback;
|
1662
1726
|
}
|
1663
1727
|
app.use=function (url,callback){
|
1664
|
-
if
|
1665
|
-
url
|
1666
|
-
|
1667
|
-
|
1668
|
-
|
1669
|
-
|
1670
|
-
|
1671
|
-
|
1728
|
+
if(typeof url === 'function' || typeof url === 'object' ){
|
1729
|
+
let plugin=url;
|
1730
|
+
let args=callback;
|
1731
|
+
if(plugin.installed){
|
1732
|
+
return app;
|
1733
|
+
}
|
1734
|
+
if (typeof plugin === 'function') {
|
1735
|
+
plugin(app, args);
|
1736
|
+
} else {
|
1737
|
+
plugin.install(app, args);
|
1738
|
+
}
|
1739
|
+
M._globle_plugin.add(plugin);
|
1740
|
+
plugin.installed = true;
|
1741
|
+
}else {
|
1742
|
+
if (Array.isArray(url)) {
|
1743
|
+
url.forEach(u=>{
|
1744
|
+
let regExp=new RegExp(u)
|
1745
|
+
G._use[u] = {url,regExp,callback};
|
1746
|
+
})
|
1747
|
+
} else {
|
1748
|
+
let regExp=new RegExp(url)
|
1749
|
+
G._use[url] = {url,regExp,callback};
|
1750
|
+
}
|
1751
|
+
}
|
1752
|
+
return app;
|
1753
|
+
}
|
1754
|
+
|
1755
|
+
app.installPlugin=async function (pluginUrl,constructorParams,pluginParams){
|
1756
|
+
if(M._globle_plugin_url_cacheMap[pluginUrl]){
|
1757
|
+
return
|
1672
1758
|
}
|
1759
|
+
M._globle_plugin_url_cacheMap[pluginUrl]=pluginUrl;
|
1760
|
+
const Plugin= await M.require(pluginUrl);
|
1761
|
+
const plugin= new Plugin(constructorParams);
|
1762
|
+
app.use(plugin,pluginParams)
|
1673
1763
|
}
|
1764
|
+
|
1674
1765
|
/**
|
1675
1766
|
* 注册get请求
|
1676
1767
|
*/
|
@@ -1764,9 +1855,8 @@ M.failResult=(msg,code,d)=>{
|
|
1764
1855
|
app.set("gloable_exception_handle",(err,req,res)=>{
|
1765
1856
|
console.error(err.stack)
|
1766
1857
|
if (res && !res.alreadySend) {
|
1767
|
-
|
1768
|
-
res.
|
1769
|
-
res.end();
|
1858
|
+
// res.writeHead(500, { "Content-Type": "text/j;charset='utf-8'" });
|
1859
|
+
res.send(M.result(err.message,false,-1));
|
1770
1860
|
}
|
1771
1861
|
})
|
1772
1862
|
|
@@ -1810,7 +1900,6 @@ M.failResult=(msg,code,d)=>{
|
|
1810
1900
|
console.log("listen on port:" + port);
|
1811
1901
|
return server;
|
1812
1902
|
}
|
1813
|
-
|
1814
1903
|
return app;
|
1815
1904
|
}
|
1816
1905
|
M["_gloable_exception_handle"]=(err)=>{
|
@@ -1963,31 +2052,96 @@ privateObj.dealUseServer = async function (req, res) {
|
|
1963
2052
|
|
1964
2053
|
/*SSE SERVER */
|
1965
2054
|
M.sseServer = function () {
|
2055
|
+
//sse 心跳
|
2056
|
+
function headBeat(){
|
2057
|
+
try {
|
2058
|
+
let clientIdList= Array.from(M._sseClientMap.keys());
|
2059
|
+
for (let i=0;i<clientIdList.length;i++){
|
2060
|
+
let clientId= clientIdList[i];
|
2061
|
+
let res= M._sseClientMap.get(clientId).res;
|
2062
|
+
let r= res.write(': \n\n');
|
2063
|
+
if(!r){
|
2064
|
+
M._sseClientMap.delete(clientId);
|
2065
|
+
M["_sse_disconnect"](clientId);
|
2066
|
+
}
|
2067
|
+
}
|
2068
|
+
}catch (e){
|
2069
|
+
console.error(e);
|
2070
|
+
}
|
2071
|
+
}
|
2072
|
+
|
2073
|
+
M["_sse_connection"]=(clientId)=>{
|
2074
|
+
console.log(clientId+" connection")
|
2075
|
+
}
|
2076
|
+
|
2077
|
+
M["_sse_disconnect"]=(clientId)=>{
|
2078
|
+
console.log(clientId+" disconnect")
|
2079
|
+
}
|
2080
|
+
|
2081
|
+
M["_sse_send"]=(clientId,msg)=>{
|
2082
|
+
// console.log("sse_send",clientId,msg)
|
2083
|
+
}
|
2084
|
+
|
2085
|
+
event.on('sseSendMsg', function (sendData,clientId,eventName="slide",id=+new Date()) {
|
2086
|
+
let sendObj={event:eventName,id:id,data:JSON.stringify(sendData),retry:10000}
|
2087
|
+
if(!clientId){
|
2088
|
+
let clientIdList= Array.from(M._sseClientMap.keys())
|
2089
|
+
for (let i=0;i<clientIdList.length;i++){
|
2090
|
+
let clientId= clientIdList[i];
|
2091
|
+
let res=M._sseClientMap.get(clientId)?M._sseClientMap.get(clientId).res:null;
|
2092
|
+
if(res==null){
|
2093
|
+
continue;
|
2094
|
+
}
|
2095
|
+
res.write(`event: ${sendObj.event}\n`); // 事件类型
|
2096
|
+
res.write(`id: ${sendObj.id}\n`); // 消息 ID
|
2097
|
+
res.write(`data: ${sendObj.data}\n`); // 消息数据
|
2098
|
+
res.write(`retry: ${sendObj.retry}\n`); // 重连时间
|
2099
|
+
res.write('\n\n'); // 消息结束
|
2100
|
+
}
|
2101
|
+
}else {
|
2102
|
+
let res=M._sseClientMap.get(clientId)?M._sseClientMap.get(clientId).res:null;
|
2103
|
+
if(res==null){
|
2104
|
+
return
|
2105
|
+
}
|
2106
|
+
res.write(`event: ${sendObj.event}\n`); // 事件类型
|
2107
|
+
res.write(`id: ${sendObj.id}\n`); // 消息 ID
|
2108
|
+
res.write(`data: ${sendObj.data}\n`); // 消息数据
|
2109
|
+
res.write(`retry: ${sendObj.retry}\n`); // 重连时间
|
2110
|
+
res.write('\n\n'); // 消息结束
|
2111
|
+
}
|
2112
|
+
M["_sse_send"](clientId,sendObj);
|
2113
|
+
})
|
2114
|
+
setInterval(() => {
|
2115
|
+
headBeat();
|
2116
|
+
}, M._sseHeatTime);
|
2117
|
+
|
1966
2118
|
let app = function (req, res) {
|
1967
|
-
|
2119
|
+
headBeat();
|
2120
|
+
let clientId=req.params.clientId;
|
2121
|
+
let connectionRes= M["_sse_connection"](clientId);
|
2122
|
+
if(connectionRes==false){
|
2123
|
+
return;
|
2124
|
+
}
|
2125
|
+
M._sseClientMap.set(clientId,{res});
|
1968
2126
|
res.writeHead(200, {
|
1969
2127
|
'Content-Type': 'text/event-stream',
|
1970
2128
|
'Cache-Control': 'no-cache',
|
1971
2129
|
'Connection': 'keep-alive',
|
1972
2130
|
'Access-Control-Allow-Origin': '*',
|
1973
2131
|
});
|
1974
|
-
event.removeAllListeners("sseSendMsg")
|
1975
|
-
event.on('sseSendMsg', function (r) {
|
1976
|
-
res.write('event: slide\n'); // 事件类型
|
1977
|
-
res.write(`id: ${+new Date()}\n`); // 消息 ID
|
1978
|
-
res.write(`data: ${r}\n`); // 消息数据
|
1979
|
-
res.write('retry: 10000\n'); // 重连时间
|
1980
|
-
res.write('\n\n'); // 消息结束
|
1981
|
-
})
|
1982
|
-
// 发送注释保持长连接
|
1983
|
-
setInterval(() => {
|
1984
|
-
res.write(': \n\n');
|
1985
|
-
}, 12000);
|
1986
|
-
|
1987
2132
|
};
|
1988
|
-
app.send = function (msg) {
|
1989
|
-
event.emit('sseSendMsg', msg);
|
2133
|
+
app.send = function (msg,clientId,eventName,id) {
|
2134
|
+
event.emit('sseSendMsg', msg,clientId,eventName,id);
|
1990
2135
|
}
|
2136
|
+
|
2137
|
+
app.call = function (eventName,msg,clientId,id) {
|
2138
|
+
event.emit('sseSendMsg', msg,clientId,eventName,id);
|
2139
|
+
}
|
2140
|
+
|
2141
|
+
app.set = function (k, v) {
|
2142
|
+
M["_" + k] = v;
|
2143
|
+
}
|
2144
|
+
|
1991
2145
|
app.listen = function (port) {
|
1992
2146
|
let serverObj = http.createServer(app).listen(port);
|
1993
2147
|
app.serverObj = serverObj;
|
package/ming_node.md
CHANGED
@@ -1001,7 +1001,7 @@ console.log(new Date().format("yyyy-MM-dd"))
|
|
1001
1001
|
在含有static文件夹的目录执行下面命令,static便作为web根目录
|
1002
1002
|
```bash
|
1003
1003
|
#node
|
1004
|
-
curl https://minglie.github.io/js/
|
1004
|
+
curl https://minglie.github.io/js/MemoryBaseRpcApi.js > MemoryBaseRpcApi.js && node MemoryBaseRpcApi.js
|
1005
1005
|
#python
|
1006
1006
|
curl https://minglie.github.io/python/index.py > index.py && python index.py
|
1007
1007
|
|
@@ -1009,12 +1009,12 @@ curl https://minglie.github.io/python/index.py > index.py && python index.py
|
|
1009
1009
|
git clone https://github.com/minglie/ming_mockServer.git && cd ming_mockServer && npm i && npm run start
|
1010
1010
|
|
1011
1011
|
#curl启动ming_mockServer0
|
1012
|
-
curl https://minglie.gitee.io/mingpage/static/js/ming_mockServer0.js >
|
1012
|
+
curl https://minglie.gitee.io/mingpage/static/js/ming_mockServer0.js > MemoryBaseRpcApi.js && node MemoryBaseRpcApi.js
|
1013
1013
|
|
1014
1014
|
```
|
1015
1015
|
## 当前目录静态页
|
1016
1016
|
```javascript
|
1017
|
-
curl https://minglie.gitee.io/mingpage/static/js/index_cur.js >
|
1017
|
+
curl https://minglie.gitee.io/mingpage/static/js/index_cur.js > MemoryBaseRpcApi.js && node MemoryBaseRpcApi.js
|
1018
1018
|
```
|
1019
1019
|
```javascript
|
1020
1020
|
+async function(){
|
@@ -1082,7 +1082,7 @@ app.post("/axios", async (req, res) => {
|
|
1082
1082
|
## 写web接口最快捷的方式ming_share_edit
|
1083
1083
|
运行脚本, 访问 [http://localhost:8888/](http://localhost:8888/)
|
1084
1084
|
```bash
|
1085
|
-
curl https://minglie.gitee.io/mi/i2.js >
|
1085
|
+
curl https://minglie.gitee.io/mi/i2.js > MemoryBaseRpcApi.js && node MemoryBaseRpcApi.js
|
1086
1086
|
```
|
1087
1087
|
![image.png](https://ming-bucket-01.oss-cn-beijing.aliyuncs.com/yuque/1591515706715-62d3b6f6-f113-497f-a532-63792180cd09.png#align=left&display=inline&height=506&margin=%5Bobject%20Object%5D&name=image.png&originHeight=506&originWidth=720&size=65942&status=done&style=none&width=720)
|
1088
1088
|
## ming_api_mock
|