ming_node 2.2.0 → 2.2.6
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 +19 -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/sseserver.js +44 -0
- package/beforeTest/static/ssetest.html +21 -0
- package/index.js +187 -49
- 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,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 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);
|
@@ -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.
|
5
|
+
* Date :2021.12.01
|
6
|
+
* version :2.2.6
|
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
|
}
|
@@ -45,7 +48,16 @@
|
|
45
48
|
M.remoteStaticPathEnable = true;
|
46
49
|
//代理服务器配置
|
47
50
|
M.proxyHost = "http://127.0.0.1:8888"
|
48
|
-
M.proxyHost = ""
|
51
|
+
M.proxyHost = "";
|
52
|
+
|
53
|
+
M.setModule=function (key,module){
|
54
|
+
M._moduleMap.set(key,module);
|
55
|
+
}
|
56
|
+
M.getModule=function (key){
|
57
|
+
M._moduleMap.get(key);
|
58
|
+
}
|
59
|
+
|
60
|
+
|
49
61
|
/**
|
50
62
|
* ----------------------客户端START--------------------------------------------
|
51
63
|
*/
|
@@ -655,13 +667,14 @@ M.request.put=M.put;
|
|
655
667
|
}
|
656
668
|
}
|
657
669
|
M.writeObjToFile(file, d);
|
670
|
+
return d_num;
|
658
671
|
}
|
659
672
|
|
660
673
|
M.updateObjByIdFile = function (file, obj) {
|
661
674
|
var d = M.getObjByFile(file);
|
662
675
|
for (let i = 0; i < d.length; i++) {
|
663
676
|
if (d[i].id == obj.id) {
|
664
|
-
d.splice(i, 1, obj);
|
677
|
+
d.splice(i, 1, Object.assign(d[i],obj));
|
665
678
|
break;
|
666
679
|
}
|
667
680
|
}
|
@@ -675,17 +688,28 @@ M.request.put=M.put;
|
|
675
688
|
}
|
676
689
|
}
|
677
690
|
}
|
678
|
-
M.listAllObjByPropFile = function (file,
|
679
|
-
|
680
|
-
let
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
691
|
+
M.listAllObjByPropFile = function (file, caseObj) {
|
692
|
+
var d = M.getObjByFile(file);
|
693
|
+
let o_keys = Object.keys(caseObj);
|
694
|
+
if (caseObj && o_keys.length>0) {
|
695
|
+
let r_list = [];
|
696
|
+
let o_vals = Object.values(caseObj);
|
697
|
+
for (let i = 0; i < d.length; i++) {
|
698
|
+
let s=0;
|
699
|
+
for (let j=0;j<o_keys.length;j++){
|
700
|
+
if (d[i][o_keys[j]] != o_vals[j]) {
|
701
|
+
break
|
702
|
+
}
|
703
|
+
s++;
|
704
|
+
}
|
705
|
+
if(s==o_keys.length){
|
706
|
+
r_list.push(d[i]);
|
707
|
+
}
|
686
708
|
}
|
709
|
+
return r_list;
|
710
|
+
} else {
|
711
|
+
return d;
|
687
712
|
}
|
688
|
-
return r_list;
|
689
713
|
}
|
690
714
|
/**
|
691
715
|
* 文件型数据库第二层封装
|
@@ -974,6 +998,7 @@ M.getMySql = function (dbConfig) {
|
|
974
998
|
timezone: "08:00"
|
975
999
|
}
|
976
1000
|
var Db = {};
|
1001
|
+
Db.dbConfig=defaultDbConfig;
|
977
1002
|
console.log("connect mysql", defaultDbConfig)
|
978
1003
|
var pool = mysql.createPool(defaultDbConfig);
|
979
1004
|
Db.pool=pool;
|
@@ -1207,8 +1232,9 @@ M.getMongoDB = function (dbConfig) {
|
|
1207
1232
|
}
|
1208
1233
|
|
1209
1234
|
}
|
1210
|
-
|
1211
|
-
|
1235
|
+
MingMongoClient.ObjectID=(id)=> new ObjectID(id)
|
1236
|
+
let Db=MingMongoClient;
|
1237
|
+
Db.dbConfig=Config;
|
1212
1238
|
MingMongoClient.collectionName="test"
|
1213
1239
|
M.mongoDb=Db;
|
1214
1240
|
return Db;
|
@@ -1356,6 +1382,40 @@ M.failResult=(msg,code,d)=>{
|
|
1356
1382
|
M.randomStr = function () {
|
1357
1383
|
return (Math.random().toString(36) + new Date().getTime()).slice(2);
|
1358
1384
|
}
|
1385
|
+
|
1386
|
+
M.urlStringify = function (obj) {
|
1387
|
+
if (obj !== null && typeof obj === 'object') {
|
1388
|
+
var keys = Object.keys(obj);
|
1389
|
+
var len = keys.length;
|
1390
|
+
var flast = len - 1;
|
1391
|
+
var fields = '';
|
1392
|
+
for (var i = 0; i < len; ++i) {
|
1393
|
+
var k = keys[i];
|
1394
|
+
var v = obj[k];
|
1395
|
+
var ks = k + "=";
|
1396
|
+
fields += ks + v;
|
1397
|
+
if (i < flast)
|
1398
|
+
fields += "&";
|
1399
|
+
}
|
1400
|
+
return fields;
|
1401
|
+
}
|
1402
|
+
return '';
|
1403
|
+
};
|
1404
|
+
|
1405
|
+
M.urlParse = function (url) {
|
1406
|
+
url = url.substr(url.indexOf("?") + 1);
|
1407
|
+
var t, n, r, i = url, s = {};
|
1408
|
+
t = i.split("&"),
|
1409
|
+
r = null,
|
1410
|
+
n = null;
|
1411
|
+
for (var o in t) {
|
1412
|
+
var u = t[o].indexOf("=");
|
1413
|
+
u !== -1 && (r = t[o].substr(0, u),
|
1414
|
+
n = t[o].substr(u + 1),
|
1415
|
+
s[r] = n)
|
1416
|
+
}
|
1417
|
+
return s
|
1418
|
+
};
|
1359
1419
|
|
1360
1420
|
/**
|
1361
1421
|
* 异常处理钩子
|
@@ -1661,15 +1721,30 @@ M.failResult=(msg,code,d)=>{
|
|
1661
1721
|
G._server = callback;
|
1662
1722
|
}
|
1663
1723
|
app.use=function (url,callback){
|
1664
|
-
if
|
1665
|
-
url
|
1666
|
-
|
1667
|
-
|
1668
|
-
|
1669
|
-
|
1670
|
-
|
1671
|
-
|
1724
|
+
if(typeof url === 'function' || typeof url === 'object' ){
|
1725
|
+
let plugin=url;
|
1726
|
+
let args=callback;
|
1727
|
+
if(plugin.installed){
|
1728
|
+
return app;
|
1729
|
+
}
|
1730
|
+
if (typeof plugin === 'function') {
|
1731
|
+
plugin(app, args);
|
1732
|
+
} else {
|
1733
|
+
plugin.install(app, args);
|
1734
|
+
}
|
1735
|
+
plugin.installed = true;
|
1736
|
+
}else {
|
1737
|
+
if (Array.isArray(url)) {
|
1738
|
+
url.forEach(u=>{
|
1739
|
+
let regExp=new RegExp(u)
|
1740
|
+
G._use[u] = {url,regExp,callback};
|
1741
|
+
})
|
1742
|
+
} else {
|
1743
|
+
let regExp=new RegExp(url)
|
1744
|
+
G._use[url] = {url,regExp,callback};
|
1745
|
+
}
|
1672
1746
|
}
|
1747
|
+
return app;
|
1673
1748
|
}
|
1674
1749
|
/**
|
1675
1750
|
* 注册get请求
|
@@ -1764,9 +1839,8 @@ M.failResult=(msg,code,d)=>{
|
|
1764
1839
|
app.set("gloable_exception_handle",(err,req,res)=>{
|
1765
1840
|
console.error(err.stack)
|
1766
1841
|
if (res && !res.alreadySend) {
|
1767
|
-
|
1768
|
-
res.
|
1769
|
-
res.end();
|
1842
|
+
// res.writeHead(500, { "Content-Type": "text/j;charset='utf-8'" });
|
1843
|
+
res.send(M.result(err.message,false,-1));
|
1770
1844
|
}
|
1771
1845
|
})
|
1772
1846
|
|
@@ -1802,15 +1876,14 @@ M.failResult=(msg,code,d)=>{
|
|
1802
1876
|
);
|
1803
1877
|
res.end();
|
1804
1878
|
})
|
1805
|
-
|
1806
|
-
|
1807
|
-
|
1879
|
+
|
1880
|
+
|
1881
|
+
|
1808
1882
|
app.listen = function (port) {
|
1809
|
-
http.createServer(app).listen(port);
|
1883
|
+
const server= http.createServer(app).listen(port);
|
1810
1884
|
console.log("listen on port:" + port);
|
1811
|
-
return
|
1885
|
+
return server;
|
1812
1886
|
}
|
1813
|
-
|
1814
1887
|
return app;
|
1815
1888
|
}
|
1816
1889
|
M["_gloable_exception_handle"]=(err)=>{
|
@@ -1963,31 +2036,96 @@ privateObj.dealUseServer = async function (req, res) {
|
|
1963
2036
|
|
1964
2037
|
/*SSE SERVER */
|
1965
2038
|
M.sseServer = function () {
|
2039
|
+
//sse 心跳
|
2040
|
+
function headBeat(){
|
2041
|
+
try {
|
2042
|
+
let clientIdList= Array.from(M._sseClientMap.keys());
|
2043
|
+
for (let i=0;i<clientIdList.length;i++){
|
2044
|
+
let clientId= clientIdList[i];
|
2045
|
+
let res= M._sseClientMap.get(clientId).res;
|
2046
|
+
let r= res.write(': \n\n');
|
2047
|
+
if(!r){
|
2048
|
+
M._sseClientMap.delete(clientId);
|
2049
|
+
M["_sse_disconnect"](clientId);
|
2050
|
+
}
|
2051
|
+
}
|
2052
|
+
}catch (e){
|
2053
|
+
console.error(e);
|
2054
|
+
}
|
2055
|
+
}
|
2056
|
+
|
2057
|
+
M["_sse_connection"]=(clientId)=>{
|
2058
|
+
console.log(clientId+" connection")
|
2059
|
+
}
|
2060
|
+
|
2061
|
+
M["_sse_disconnect"]=(clientId)=>{
|
2062
|
+
console.log(clientId+" disconnect")
|
2063
|
+
}
|
2064
|
+
|
2065
|
+
M["_sse_send"]=(clientId,msg)=>{
|
2066
|
+
// console.log("sse_send",clientId,msg)
|
2067
|
+
}
|
2068
|
+
|
2069
|
+
event.on('sseSendMsg', function (sendData,clientId,eventName="slide",id=+new Date()) {
|
2070
|
+
let sendObj={event:eventName,id:id,data:JSON.stringify(sendData),retry:10000}
|
2071
|
+
if(!clientId){
|
2072
|
+
let clientIdList= Array.from(M._sseClientMap.keys())
|
2073
|
+
for (let i=0;i<clientIdList.length;i++){
|
2074
|
+
let clientId= clientIdList[i];
|
2075
|
+
let res=M._sseClientMap.get(clientId)?M._sseClientMap.get(clientId).res:null;
|
2076
|
+
if(res==null){
|
2077
|
+
continue;
|
2078
|
+
}
|
2079
|
+
res.write(`event: ${sendObj.event}\n`); // 事件类型
|
2080
|
+
res.write(`id: ${sendObj.id}\n`); // 消息 ID
|
2081
|
+
res.write(`data: ${sendObj.data}\n`); // 消息数据
|
2082
|
+
res.write(`retry: ${sendObj.retry}\n`); // 重连时间
|
2083
|
+
res.write('\n\n'); // 消息结束
|
2084
|
+
}
|
2085
|
+
}else {
|
2086
|
+
let res=M._sseClientMap.get(clientId)?M._sseClientMap.get(clientId).res:null;
|
2087
|
+
if(res==null){
|
2088
|
+
return
|
2089
|
+
}
|
2090
|
+
res.write(`event: ${sendObj.event}\n`); // 事件类型
|
2091
|
+
res.write(`id: ${sendObj.id}\n`); // 消息 ID
|
2092
|
+
res.write(`data: ${sendObj.data}\n`); // 消息数据
|
2093
|
+
res.write(`retry: ${sendObj.retry}\n`); // 重连时间
|
2094
|
+
res.write('\n\n'); // 消息结束
|
2095
|
+
}
|
2096
|
+
M["_sse_send"](clientId,sendObj);
|
2097
|
+
})
|
2098
|
+
setInterval(() => {
|
2099
|
+
headBeat();
|
2100
|
+
}, M._sseHeatTime);
|
2101
|
+
|
1966
2102
|
let app = function (req, res) {
|
1967
|
-
|
2103
|
+
headBeat();
|
2104
|
+
let clientId=req.params.clientId;
|
2105
|
+
let connectionRes= M["_sse_connection"](clientId);
|
2106
|
+
if(connectionRes==false){
|
2107
|
+
return;
|
2108
|
+
}
|
2109
|
+
M._sseClientMap.set(clientId,{res});
|
1968
2110
|
res.writeHead(200, {
|
1969
2111
|
'Content-Type': 'text/event-stream',
|
1970
2112
|
'Cache-Control': 'no-cache',
|
1971
2113
|
'Connection': 'keep-alive',
|
1972
2114
|
'Access-Control-Allow-Origin': '*',
|
1973
2115
|
});
|
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
2116
|
};
|
1988
|
-
app.send = function (msg) {
|
1989
|
-
event.emit('sseSendMsg', msg);
|
2117
|
+
app.send = function (msg,clientId,eventName,id) {
|
2118
|
+
event.emit('sseSendMsg', msg,clientId,eventName,id);
|
2119
|
+
}
|
2120
|
+
|
2121
|
+
app.call = function (eventName,msg,clientId,id) {
|
2122
|
+
event.emit('sseSendMsg', msg,clientId,eventName,id);
|
2123
|
+
}
|
2124
|
+
|
2125
|
+
app.set = function (k, v) {
|
2126
|
+
M["_" + k] = v;
|
1990
2127
|
}
|
2128
|
+
|
1991
2129
|
app.listen = function (port) {
|
1992
2130
|
let serverObj = http.createServer(app).listen(port);
|
1993
2131
|
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
|