nodejs-local-ipc
Advanced tools
+2
-2
| { | ||
| "name": "nodejs-local-ipc", | ||
| "version": "1.0.9", | ||
| "description": "实现nodejs中兄弟进程的通信", | ||
| "version": "1.5.0", | ||
| "description": "使用自定义的应用层协议,实现nodejs进程间通信", | ||
| "main": "src/index.js", | ||
@@ -6,0 +6,0 @@ "directories": { |
+8
-1
| # nodejs-ipc | ||
| 使用自定义的应用层协议,实现nodejs兄弟进程通信,windows下通过tcp、非windows下通过unix域,支持在长连接中并行发送多个请求和响应处理。使用例子见test目录 | ||
| 使用自定义的应用层协议,实现nodejs进程间通信 | ||
| <br/> | ||
| 本机进程间:<br/> | ||
| windows下通过tcp、非windows下通过unix域<br/> | ||
| 远程进程间:<br/> | ||
| tcp<br/> | ||
| 支持在长连接中并行发送一个或多个请求和响应处理。通过end函数可以断开连接,nodejs默认会断开读写两端,如果一端调用end后,另一端还希望给另一端发送数据,则设置allowHalfOpen:true,使用例子见test目录。 | ||
+23
-37
@@ -10,42 +10,28 @@ const net = require('net'); | ||
| super(); | ||
| this.options = { ...options }; | ||
| this.socket = null; | ||
| this.fsm = new FSM({ | ||
| cb: (packet) => { | ||
| this.emit('message', packet); | ||
| } | ||
| }) | ||
| this.options = this.formatOptions(options); | ||
| const socket = net.connect(this.options); | ||
| socket.on('error', (error) => { | ||
| console.error(error); | ||
| }); | ||
| socket.send = socket.write; | ||
| const fsm = new FSM({ | ||
| cb: (packet) => { | ||
| socket.emit('message', packet); | ||
| } | ||
| }); | ||
| socket.on('data', fsm.run.bind(fsm)); | ||
| return socket; | ||
| } | ||
| initOnce() { | ||
| if (!this.socket) { | ||
| if (os.platform() === 'win32' || this.options.isRPC) { | ||
| !~~this.options.port && (this.options.port = this.options.isRPC ? 80 : port); | ||
| delete this.options.path; | ||
| } else { | ||
| !this.options.path && (this.options.path = path); | ||
| delete this.options.host; | ||
| delete this.options.port; | ||
| } | ||
| this.socket = net.connect({allowHalfOpen: true, ...this.options}); | ||
| this.socket.on('data', this.fsm.run.bind(this.fsm)); | ||
| this.socket.on('end', () => { | ||
| // 触发end事件 | ||
| this.emit('end'); | ||
| // 用户侧没有关闭写端,则默认关闭 | ||
| !this.socket.writableEnded && this.options.autoEnd !== false && this.socket.end(); | ||
| }); | ||
| this.socket.on('error', (e) => { | ||
| this.listenerCount('error') > 0 && this.emit('error', e); | ||
| }); | ||
| formatOptions(_options) { | ||
| const options = { ..._options }; | ||
| if (os.platform() === 'win32' || options.isRPC) { | ||
| !~~options.port && (options.port = options.isRPC ? 80 : port); | ||
| delete options.path; | ||
| } else { | ||
| !options.path && (options.path = path); | ||
| delete options.host; | ||
| delete options.port; | ||
| } | ||
| return options; | ||
| } | ||
| send(data) { | ||
| this.initOnce(); | ||
| this.socket.write(data); | ||
| return this; | ||
| } | ||
| end(data) { | ||
| this.initOnce(); | ||
| this.socket.end(data); | ||
| } | ||
| } | ||
@@ -52,0 +38,0 @@ module.exports = { |
@@ -29,2 +29,7 @@ class RequestManager { | ||
| } | ||
| } | ||
| execAll(data) { | ||
| for (const [key] of Object.entries(this.map)) { | ||
| this.exec(key, data); | ||
| } | ||
| } | ||
@@ -31,0 +36,0 @@ startPollTimeout() { |
+28
-47
@@ -8,16 +8,2 @@ const fs = require('fs'); | ||
| // Client代表一个和server建立连接的客户端 | ||
| class Client extends EventEmitter { | ||
| constructor(options) { | ||
| super(); | ||
| this.options = options; | ||
| } | ||
| send(data) { | ||
| this.options.client.write(data); | ||
| } | ||
| end(data) { | ||
| this.options.client.end(data); | ||
| } | ||
| } | ||
| class Server extends EventEmitter { | ||
@@ -27,45 +13,40 @@ constructor(options, connectionListener) { | ||
| if (typeof options === 'function') { | ||
| options = {}; | ||
| connectionListener = options; | ||
| } | ||
| this.options = { ...options }; | ||
| // 根据平台处理参数 | ||
| if (os.platform() === 'win32') { | ||
| !~~this.options.port && (this.options.port = port); | ||
| delete this.options.path; | ||
| options = { | ||
| connectionListener, | ||
| }; | ||
| } else { | ||
| !this.options.path && (this.options.path = path); | ||
| delete this.options.host; | ||
| delete this.options.port; | ||
| fs.existsSync(this.options.path) && fs.unlinkSync(this.options.path); | ||
| process.on('exit', () => { | ||
| fs.existsSync(this.options.path) && fs.unlinkSync(this.options.path); | ||
| }); | ||
| options = { ...options, connectionListener }; | ||
| } | ||
| this.server = net.createServer({allowHalfOpen: true}, (client) => { | ||
| const _client = new Client({client}); | ||
| typeof connectionListener === 'function' && connectionListener(_client); | ||
| this.options = this.formatOptions(options); | ||
| return net.createServer({allowHalfOpen: this.options.allowHalfOpen === true}, (client) => { | ||
| client.send = client.write; | ||
| const fsm = new FSM({ | ||
| cb: function(packet) { | ||
| _client.emit('message', packet); | ||
| client.emit('message', packet); | ||
| } | ||
| }) | ||
| client.on('data', fsm.run.bind(fsm)); | ||
| client.on('end', () => { | ||
| // 触发end事件 | ||
| _client.emit('end'); | ||
| // 用户侧没有关闭写端,则默认关闭 | ||
| !client.writableEnded && this.options.autoEnd !== false && client.end(); | ||
| }); | ||
| client.on('error', (error) => { | ||
| _client.listenerCount('error') > 0 && _client.emit('error', error); | ||
| console.error(error); | ||
| }); | ||
| }); | ||
| this.server.listen(this.options, () => { | ||
| this.emit('listen'); | ||
| }); | ||
| this.server.on('error', (error) => { | ||
| this.listenerCount('error') > 0 && this.emit('error', error); | ||
| }); | ||
| typeof this.options.connectionListener === 'function' && this.options.connectionListener(client); | ||
| }).listen(this.options); | ||
| } | ||
| formatOptions(_options) { | ||
| const options = { ..._options }; | ||
| // 根据平台处理参数 | ||
| if (os.platform() === 'win32') { | ||
| !~~options.port && (options.port = port); | ||
| delete options.path; | ||
| } else { | ||
| !options.path && (options.path = path); | ||
| delete options.host; | ||
| delete options.port; | ||
| fs.existsSync(options.path) && fs.unlinkSync(options.path); | ||
| process.on('exit', () => { | ||
| fs.existsSync(options.path) && fs.unlinkSync(options.path); | ||
| }); | ||
| } | ||
| return options; | ||
| } | ||
| } | ||
@@ -72,0 +53,0 @@ |
10
233.33%6966
-8.77%153
-15%