ming_node 3.0.0 → 3.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/beforeTest/a.csv +3 -0
- package/beforeTest/clusterStartTest.js +8 -0
- package/beforeTest/net/TcpClientTest.js +10 -0
- package/beforeTest/net/TcpServerTest.js +16 -0
- package/beforeTest/net/UdpClientTest.js +10 -0
- package/beforeTest/net/UdpServerTest.js +9 -0
- package/beforeTest/readcvv.js +11 -0
- package/beforeTest/restartest.js +1 -0
- package/beforeTest/restartestMain.js +7 -0
- package/beforeTest/static/a.json +3 -0
- package/beforeTest/static/a.json5 +3 -0
- package/beforeTest/test.js +2 -5
- package/index.js +165 -136
- package/module/net/TcpClient.js +64 -0
- package/module/net/TcpServer.js +108 -0
- package/module/net/UdpClient.js +68 -0
- package/module/net/UdpServer.js +64 -0
- package/module/net/es/TcpServer.js +109 -0
- package/package.json +1 -1
- package/utils/common/DataConvert.js +30 -0
- package/utils/nodemon/clusterStart.js +72 -0
@@ -0,0 +1,64 @@
|
|
1
|
+
const net = require('net');
|
2
|
+
|
3
|
+
class TcpClient {
|
4
|
+
static _EventCallBack={};
|
5
|
+
constructor(remoteIp,remotePort) {
|
6
|
+
this.connected=false;
|
7
|
+
this.remoteIp=remoteIp;
|
8
|
+
this.remotePort=remotePort;
|
9
|
+
}
|
10
|
+
set(url, callback) {
|
11
|
+
TcpClient._EventCallBack[url]=callback;
|
12
|
+
}
|
13
|
+
|
14
|
+
connect(){
|
15
|
+
let that=this;
|
16
|
+
if(that.connected){
|
17
|
+
return
|
18
|
+
}
|
19
|
+
this.client = net.createConnection({
|
20
|
+
port:this.remotePort ,
|
21
|
+
host:this.remoteIp
|
22
|
+
});
|
23
|
+
this.connected=true;
|
24
|
+
//当套字节与服务端连接成功时触发connect事件
|
25
|
+
this.client.on('connect', () =>{
|
26
|
+
that.connected=true;
|
27
|
+
TcpClient._EventCallBack['connect']? TcpClient._EventCallBack['connect'](): console.log("connect success");
|
28
|
+
});
|
29
|
+
//使用data事件监听服务端响应过来的数据
|
30
|
+
this.client.on('data', (chunk) => {
|
31
|
+
let req={};
|
32
|
+
req.remoteIp=that.remoteIp;
|
33
|
+
req.remotePort=that.remotePort;
|
34
|
+
req.params=chunk;
|
35
|
+
let res={};
|
36
|
+
res.send=function (v){
|
37
|
+
if(Array.isArray(v)){
|
38
|
+
v=new Buffer(v)
|
39
|
+
}
|
40
|
+
that.client.write(v)
|
41
|
+
}
|
42
|
+
TcpClient._EventCallBack['data'](req,res);
|
43
|
+
|
44
|
+
});
|
45
|
+
this.client.on('error', (err)=>{
|
46
|
+
TcpClient._EventCallBack["error"]?TcpClient._EventCallBack["error"](client): console.error(err);
|
47
|
+
});
|
48
|
+
|
49
|
+
|
50
|
+
this.client.on('close', ()=>{
|
51
|
+
that.connected=false;
|
52
|
+
TcpClient._EventCallBack["error"]?TcpClient._EventCallBack["error"]():console.log("close");
|
53
|
+
});
|
54
|
+
}
|
55
|
+
|
56
|
+
send(buffer){
|
57
|
+
if(this.connected){
|
58
|
+
this.client.write(buffer);
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
}
|
63
|
+
|
64
|
+
module.exports = TcpClient;
|
@@ -0,0 +1,108 @@
|
|
1
|
+
const net = require('net');
|
2
|
+
const os = require("os");
|
3
|
+
class TcpServer {
|
4
|
+
static _EventCallBack={};
|
5
|
+
static socketClientMap={};
|
6
|
+
set(url, callback) {
|
7
|
+
TcpServer._EventCallBack[url]=callback;
|
8
|
+
}
|
9
|
+
constructor() {
|
10
|
+
this.localAddress=this.getIpAddress();
|
11
|
+
}
|
12
|
+
getIpAddress(){
|
13
|
+
/**os.networkInterfaces() 返回一个对象,该对象包含已分配了网络地址的网络接口 */
|
14
|
+
var interfaces = os.networkInterfaces();
|
15
|
+
for (var devName in interfaces) {
|
16
|
+
var iface = interfaces[devName];
|
17
|
+
for (var i = 0; i < iface.length; i++) {
|
18
|
+
var alias = iface[i];
|
19
|
+
if (
|
20
|
+
alias.family === "IPv4" &&
|
21
|
+
alias.address !== "127.0.0.1" &&
|
22
|
+
!alias.internal
|
23
|
+
) {
|
24
|
+
return alias.address;
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
|
31
|
+
listen(port){
|
32
|
+
let that=this;
|
33
|
+
this.localPort=port;
|
34
|
+
this.socketServer = net.createServer(function (client) {
|
35
|
+
let endPoint= client.remoteAddress+":"+client.remotePort;
|
36
|
+
TcpServer.socketClientMap[endPoint]=client;
|
37
|
+
let req={};
|
38
|
+
req.remoteIp=client.remoteAddress;
|
39
|
+
req.remotePort=client.remotePort;
|
40
|
+
TcpServer._EventCallBack['connect']? TcpServer._EventCallBack['connect'](req,null): console.log(endPoint+"connected");
|
41
|
+
|
42
|
+
// 接收客户端的数据
|
43
|
+
client.on('data', function (data) {
|
44
|
+
let req={};
|
45
|
+
req.remoteIp=client.remoteAddress;
|
46
|
+
req.remotePort=client.remotePort;
|
47
|
+
req.params=data;
|
48
|
+
let res={};
|
49
|
+
res.send=function (v){
|
50
|
+
if(Array.isArray(v)){
|
51
|
+
v=new Buffer(v)
|
52
|
+
}
|
53
|
+
client.write(v)
|
54
|
+
}
|
55
|
+
TcpServer._EventCallBack['data'](req,res);
|
56
|
+
});
|
57
|
+
|
58
|
+
// 客户端连接关闭
|
59
|
+
client.on('close', function (err) {
|
60
|
+
delete TcpServer.socketClientMap[endPoint];
|
61
|
+
TcpServer._EventCallBack["close"]?TcpServer._EventCallBack["close"](client):"";
|
62
|
+
});
|
63
|
+
// 客户端连接错误
|
64
|
+
client.on('error', function (err) {
|
65
|
+
delete TcpServer.socketClientMap[endPoint];
|
66
|
+
TcpServer._EventCallBack["error"]?TcpServer._EventCallBack["error"](client):"";
|
67
|
+
});
|
68
|
+
});
|
69
|
+
this.socketServer.listen(
|
70
|
+
{
|
71
|
+
port: port,
|
72
|
+
host: '0.0.0.0',
|
73
|
+
},
|
74
|
+
function () {
|
75
|
+
//console.log('Tcpserver listen listen on '+that.localPort);
|
76
|
+
}
|
77
|
+
);
|
78
|
+
|
79
|
+
//设置监听时的回调函数
|
80
|
+
this.socketServer.on('listening', function () {
|
81
|
+
const { address, port } = that.socketServer.address();
|
82
|
+
console.log('Tcpserver listen on '+port);
|
83
|
+
});
|
84
|
+
|
85
|
+
//设置关闭时的回调函数
|
86
|
+
this.socketServer.on('close', function () {
|
87
|
+
console.log('sever closed');
|
88
|
+
TcpServer.socketClientMap={};
|
89
|
+
});
|
90
|
+
|
91
|
+
//设置出错时的回调函数
|
92
|
+
this.socketServer.on('error', function () {
|
93
|
+
console.log('sever error');
|
94
|
+
TcpServer.socketClientMap={};
|
95
|
+
});
|
96
|
+
}
|
97
|
+
|
98
|
+
send(buffer){
|
99
|
+
let clientEndPointList= Object.keys(TcpServer.socketClientMap);
|
100
|
+
for (let i=0;i<clientEndPointList.length;i++){
|
101
|
+
TcpServer.socketClientMap[clientEndPointList[i]].write(buffer)
|
102
|
+
}
|
103
|
+
|
104
|
+
}
|
105
|
+
|
106
|
+
}
|
107
|
+
|
108
|
+
module.exports = TcpServer;
|
@@ -0,0 +1,68 @@
|
|
1
|
+
const dgram = require('dgram');
|
2
|
+
class UdpClient {
|
3
|
+
static _EventCallBack={};
|
4
|
+
|
5
|
+
constructor(remoteIp,remotePort) {
|
6
|
+
this.server = dgram.createSocket('udp4');
|
7
|
+
this.remotePort=remotePort ;
|
8
|
+
this.remoteIp=remoteIp;
|
9
|
+
this.port=0;
|
10
|
+
this.install();
|
11
|
+
}
|
12
|
+
|
13
|
+
set(url, callback) {
|
14
|
+
UdpClient._EventCallBack[url]=callback;
|
15
|
+
}
|
16
|
+
|
17
|
+
listen(port){
|
18
|
+
this.port=port;
|
19
|
+
this.server.bind(port);
|
20
|
+
|
21
|
+
}
|
22
|
+
send(v,remoteIp,remotePort) {
|
23
|
+
if(remoteIp){
|
24
|
+
this.remoteIp=remoteIp;
|
25
|
+
}
|
26
|
+
if(remotePort){
|
27
|
+
this.remoteIp=remotePort;
|
28
|
+
}
|
29
|
+
if(Array.isArray(v)){
|
30
|
+
v=new Buffer(v)
|
31
|
+
}
|
32
|
+
this.server.send(v,this.remotePort,this.remoteIp)
|
33
|
+
}
|
34
|
+
|
35
|
+
install(app,server){
|
36
|
+
let that=this;
|
37
|
+
this.server.on('close',()=>{
|
38
|
+
UdpClient._EventCallBack['close']? UdpClient._EventCallBack['close']():console.log('udp close');
|
39
|
+
});
|
40
|
+
|
41
|
+
this.server.on('error',(err)=>{
|
42
|
+
UdpClient._EventCallBack['error']? UdpClient._EventCallBack['error'](): console.log(err);
|
43
|
+
});
|
44
|
+
|
45
|
+
this.server.on('listening',()=>{
|
46
|
+
UdpClient._EventCallBack['listening']? UdpClient._EventCallBack['listening'](): console.log('udp listen on '+that.port);;
|
47
|
+
|
48
|
+
});
|
49
|
+
this.server.on('message',(msg,rinfo)=>{
|
50
|
+
let req={}
|
51
|
+
req.rinfo=rinfo;
|
52
|
+
req.remoteIp=rinfo.address;
|
53
|
+
req.remotePort=rinfo.port;
|
54
|
+
req.params=msg;
|
55
|
+
let res={};
|
56
|
+
res.send=function (v){
|
57
|
+
if(Array.isArray(v)){
|
58
|
+
v=new Buffer(v)
|
59
|
+
}
|
60
|
+
that.server.send(v,rinfo.port ,rinfo.address)
|
61
|
+
}
|
62
|
+
UdpClient._EventCallBack['message'](req,res)
|
63
|
+
});
|
64
|
+
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
module.exports = UdpClient;
|
@@ -0,0 +1,64 @@
|
|
1
|
+
const dgram = require('dgram');
|
2
|
+
class UdpServer {
|
3
|
+
static _EventCallBack={};
|
4
|
+
|
5
|
+
constructor() {
|
6
|
+
this.server = dgram.createSocket('udp4');
|
7
|
+
this.remotePort=0;
|
8
|
+
this.remoteIp=0;
|
9
|
+
this.port=0;
|
10
|
+
this.install();
|
11
|
+
}
|
12
|
+
|
13
|
+
set(url, callback) {
|
14
|
+
UdpServer._EventCallBack[url]=callback;
|
15
|
+
}
|
16
|
+
|
17
|
+
listen(port){
|
18
|
+
this.port=port;
|
19
|
+
this.server.bind(port);
|
20
|
+
|
21
|
+
}
|
22
|
+
send(v,remoteIp,remotePort) {
|
23
|
+
if(remoteIp){
|
24
|
+
this.remoteIp=remoteIp;
|
25
|
+
}
|
26
|
+
if(remotePort){
|
27
|
+
this.remoteIp=remotePort;
|
28
|
+
}
|
29
|
+
if(Array.isArray(v)){
|
30
|
+
v=new Buffer(v)
|
31
|
+
}
|
32
|
+
this.server.send(v,this.remotePort,this.remoteIp)
|
33
|
+
}
|
34
|
+
|
35
|
+
install(app,server){
|
36
|
+
let that=this;
|
37
|
+
this.server.on('close',()=>{
|
38
|
+
UdpServer._EventCallBack['close']? UdpServer._EventCallBack['close']():console.log('udp close');
|
39
|
+
});
|
40
|
+
|
41
|
+
this.server.on('error',(err)=>{
|
42
|
+
UdpServer._EventCallBack['error']? UdpServer._EventCallBack['error'](): console.log(err);
|
43
|
+
});
|
44
|
+
|
45
|
+
this.server.on('listening',()=>{
|
46
|
+
UdpServer._EventCallBack['listening']? UdpServer._EventCallBack['listening'](): console.log('udp listen on '+that.port);;
|
47
|
+
|
48
|
+
});
|
49
|
+
this.server.on('message',(msg,rinfo)=>{
|
50
|
+
that.remoteIp=rinfo.address;
|
51
|
+
that.remotePort=rinfo.port;
|
52
|
+
let req={}
|
53
|
+
req.rinfo=rinfo;
|
54
|
+
req.ip=rinfo.address;
|
55
|
+
req.port=rinfo.port;
|
56
|
+
req.params=msg;
|
57
|
+
let res=that;
|
58
|
+
UdpServer._EventCallBack['message'](req,res)
|
59
|
+
});
|
60
|
+
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
module.exports = UdpServer;
|
@@ -0,0 +1,109 @@
|
|
1
|
+
import net from "net"
|
2
|
+
import os from 'os'
|
3
|
+
|
4
|
+
class TcpServer {
|
5
|
+
static _EventCallBack={};
|
6
|
+
static socketClientMap={};
|
7
|
+
set(url, callback) {
|
8
|
+
TcpServer._EventCallBack[url]=callback;
|
9
|
+
}
|
10
|
+
constructor() {
|
11
|
+
this.localAddress=this.getIpAddress();
|
12
|
+
}
|
13
|
+
getIpAddress(){
|
14
|
+
/**os.networkInterfaces() 返回一个对象,该对象包含已分配了网络地址的网络接口 */
|
15
|
+
var interfaces = os.networkInterfaces();
|
16
|
+
for (var devName in interfaces) {
|
17
|
+
var iface = interfaces[devName];
|
18
|
+
for (var i = 0; i < iface.length; i++) {
|
19
|
+
var alias = iface[i];
|
20
|
+
if (
|
21
|
+
alias.family === "IPv4" &&
|
22
|
+
alias.address !== "127.0.0.1" &&
|
23
|
+
!alias.internal
|
24
|
+
) {
|
25
|
+
return alias.address;
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
|
32
|
+
listen(port){
|
33
|
+
let that=this;
|
34
|
+
this.localPort=port;
|
35
|
+
this.socketServer = net.createServer(function (client) {
|
36
|
+
let endPoint= client.remoteAddress+":"+client.remotePort;
|
37
|
+
TcpServer.socketClientMap[endPoint]=client;
|
38
|
+
let req={};
|
39
|
+
req.remoteIp=client.remoteAddress;
|
40
|
+
req.remotePort=client.remotePort;
|
41
|
+
TcpServer._EventCallBack['connect']? TcpServer._EventCallBack['connect'](req,null): console.log(endPoint+"connected");
|
42
|
+
|
43
|
+
// 接收客户端的数据
|
44
|
+
client.on('data', function (data) {
|
45
|
+
let req={};
|
46
|
+
req.remoteIp=client.remoteAddress;
|
47
|
+
req.remotePort=client.remotePort;
|
48
|
+
req.params=data;
|
49
|
+
let res={};
|
50
|
+
res.send=function (v){
|
51
|
+
if(Array.isArray(v)){
|
52
|
+
v=new Buffer(v)
|
53
|
+
}
|
54
|
+
client.write(v)
|
55
|
+
}
|
56
|
+
TcpServer._EventCallBack['data'](req,res);
|
57
|
+
});
|
58
|
+
|
59
|
+
// 客户端连接关闭
|
60
|
+
client.on('close', function (err) {
|
61
|
+
delete TcpServer.socketClientMap[endPoint];
|
62
|
+
TcpServer._EventCallBack["close"]?TcpServer._EventCallBack["close"](client):"";
|
63
|
+
});
|
64
|
+
// 客户端连接错误
|
65
|
+
client.on('error', function (err) {
|
66
|
+
delete TcpServer.socketClientMap[endPoint];
|
67
|
+
TcpServer._EventCallBack["error"]?TcpServer._EventCallBack["error"](client):"";
|
68
|
+
});
|
69
|
+
});
|
70
|
+
this.socketServer.listen(
|
71
|
+
{
|
72
|
+
port: port,
|
73
|
+
host: '0.0.0.0',
|
74
|
+
},
|
75
|
+
function () {
|
76
|
+
//console.log('Tcpserver listen listen on '+that.localPort);
|
77
|
+
}
|
78
|
+
);
|
79
|
+
|
80
|
+
//设置监听时的回调函数
|
81
|
+
this.socketServer.on('listening', function () {
|
82
|
+
const { address, port } = that.socketServer.address();
|
83
|
+
console.log('Tcpserver listen on '+port);
|
84
|
+
});
|
85
|
+
|
86
|
+
//设置关闭时的回调函数
|
87
|
+
this.socketServer.on('close', function () {
|
88
|
+
console.log('sever closed');
|
89
|
+
TcpServer.socketClientMap={};
|
90
|
+
});
|
91
|
+
|
92
|
+
//设置出错时的回调函数
|
93
|
+
this.socketServer.on('error', function () {
|
94
|
+
console.log('sever error');
|
95
|
+
TcpServer.socketClientMap={};
|
96
|
+
});
|
97
|
+
}
|
98
|
+
|
99
|
+
send(buffer){
|
100
|
+
let clientEndPointList= Object.keys(TcpServer.socketClientMap);
|
101
|
+
for (let i=0;i<clientEndPointList.length;i++){
|
102
|
+
TcpServer.socketClientMap[clientEndPointList[i]].write(buffer)
|
103
|
+
}
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
export default TcpServer;
|
package/package.json
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
const DataConvert={};
|
2
|
+
|
3
|
+
DataConvert.num2Bytes=(num,length=4)=>{
|
4
|
+
if(length==2){
|
5
|
+
return [(num&(0xFF00))>>8,num&(0x00FF)]
|
6
|
+
}
|
7
|
+
if(length==4){
|
8
|
+
return [(num&(0xFF000000))>>24,(num&(0x00FF0000))>>16,(num&(0x0000FF00))>>8,num&(0x00FF)]
|
9
|
+
}
|
10
|
+
}
|
11
|
+
|
12
|
+
DataConvert.str2Bytes=(str,length=16)=>{
|
13
|
+
let zeroBuffer=new Buffer(length);
|
14
|
+
let strBuffer=new Buffer(str);
|
15
|
+
for (let i=0;i<16;i++){
|
16
|
+
zeroBuffer[i]=strBuffer[i];
|
17
|
+
}
|
18
|
+
return zeroBuffer;
|
19
|
+
}
|
20
|
+
|
21
|
+
DataConvert.parseIpToInt=(ip)=> {
|
22
|
+
var buf = ip.split(".")
|
23
|
+
return (parseInt(buf[0]) << 24 |
|
24
|
+
parseInt(buf[1]) << 16 |
|
25
|
+
parseInt(buf[2]) << 8 |
|
26
|
+
parseInt(buf[3]))>>>0;
|
27
|
+
}
|
28
|
+
|
29
|
+
|
30
|
+
module.exports = DataConvert;
|
@@ -0,0 +1,72 @@
|
|
1
|
+
const cluster = require('cluster');
|
2
|
+
const child_process = require('child_process');
|
3
|
+
const http = require('http');
|
4
|
+
|
5
|
+
function clusterStart(mainFile,reloadPort,beforeCmd,logFun,logHeatBeat){
|
6
|
+
if (cluster.isMaster) {
|
7
|
+
console.log(`主进程 ${process.pid} 正在运行`);
|
8
|
+
// fork工作进程
|
9
|
+
forkWorkProcess();
|
10
|
+
createReloadServer();
|
11
|
+
cluster.on('exit', work => {
|
12
|
+
console.log(`工作进程 ${work.process.pid} 已退出`);
|
13
|
+
});
|
14
|
+
} else {
|
15
|
+
require(mainFile);
|
16
|
+
console.log(`工作进程 ${process.pid} 已启动`);
|
17
|
+
}
|
18
|
+
|
19
|
+
// 创建1个工作进程
|
20
|
+
function forkWorkProcess() {
|
21
|
+
const node_version_cmd=beforeCmd;
|
22
|
+
logFun("===>"+node_version_cmd)
|
23
|
+
child_process.exec(node_version_cmd, (e, stdout) => {
|
24
|
+
logFun(stdout);
|
25
|
+
setTimeout(() => {
|
26
|
+
cluster.fork();
|
27
|
+
}, 2000);
|
28
|
+
});
|
29
|
+
}
|
30
|
+
|
31
|
+
//创建监听重启命令的http服务器
|
32
|
+
function createReloadServer() {
|
33
|
+
http.createServer((req, res) => {
|
34
|
+
if (req.url === '/reload') {
|
35
|
+
if (cluster.isMaster) {
|
36
|
+
const arr = Object.keys(cluster.workers);
|
37
|
+
if (arr.length !== 0) {
|
38
|
+
forkWorkProcess();
|
39
|
+
setTimeout(() => {
|
40
|
+
const arr = Object.keys(cluster.workers);
|
41
|
+
process.kill(cluster.workers[arr[0]].process.pid);
|
42
|
+
}, 20000);
|
43
|
+
}
|
44
|
+
}
|
45
|
+
res.end('reloading');
|
46
|
+
} else {
|
47
|
+
res.end('unknow command');
|
48
|
+
}
|
49
|
+
}).listen(reloadPort);
|
50
|
+
}
|
51
|
+
|
52
|
+
// 查看log
|
53
|
+
setInterval(() => {
|
54
|
+
if (cluster.isMaster) {
|
55
|
+
const workerArr = Object.keys(cluster.workers);
|
56
|
+
// 工作进程意外退出时,fork新的工作进程
|
57
|
+
if (workerArr.length === 0) {
|
58
|
+
forkWorkProcess();
|
59
|
+
}
|
60
|
+
logFun(new Date());
|
61
|
+
logFun(workerArr);
|
62
|
+
logFun(`主进程pid:${process.pid}`);
|
63
|
+
} else {
|
64
|
+
logFun(`工作进程pid:${process.pid}`);
|
65
|
+
}
|
66
|
+
}, logHeatBeat);
|
67
|
+
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
module.exports =clusterStart;
|