分场景讲解两个 Node.js 进程间如何进行通信!

广告:宝塔Linux面板高效运维的服务器管理软件 点击【 https://www.bt.cn/p/uNLv1L 】立即购买

分场景讲解两个 Node.js 进程间如何进行通信!

Node.js进程间如何进行通信?下面本篇文章给大家介绍一下分场景介绍两个 Node.js 进程间进行通信的方法,希望对大家有所帮助!

两个 Node.js 进程之间如何进行通信呢?这里要分两种场景:

不同电脑上的两个 Node.js 进程间通信

同一台电脑上两个 Node.js 进程间通信

【推荐学习:《nodejs 教程》】

对于第一种场景,通常使用 TCP 或 HTTP 进行通信,而对于第二种场景,又分为两种子场景:

Node.js 进程和自己创建的 Node.js 子进程通信

Node.js 进程和另外不相关的 Node.js 进程通信

前者可以使用内置的 IPC 通信通道,后者可以使用自定义管道,接下来进行详细介绍:

不同电脑上的两个 Node.js 进程间通信

要想进行通信,首先得搞清楚如何标识网络中的进程?网络层的 ip 地址可以唯一标识网络中的主机,而传输层的协议和端口可以唯一标识主机中的应用程序(进程),这样利用三元组(ip 地址,协议,端口)就可以标识网络的进程了。

使用 TCP 套接字

TCP 套接字(socket)是一种基于 TCP/IP 协议的通信方式,可以让通过网络连接的计算机上的进程进行通信。一个作为 server 另一个作为 client,server.js 代码如下:

const net = require('net')const server = net.createServer(socket => {  console.log('socket connected')  socket.on('close', () => console.log('socket disconnected'))  socket.on('error', err => console.error(err.message))  socket.on('data', data => {    console.log(`receive: ${data}`)    socket.write(data)    console.log(`send: ${data}`)  })})server.listen(8888)
登录后复制

client.js 代码:

const net = require('net')const client = net.connect(8888, '192.168.10.105')client.on('connect', () => console.log('connected.'))client.on('data', data => console.log(`receive: ${data}`))client.on('end', () => console.log('disconnected.'))client.on('error', err => console.error(err.message))setInterval(() => {  const msg = 'hello'  console.log(`send: ${msg}`)  client.write(msg)}, 3000)
登录后复制

运行效果:

$ node server.jsclient connectedreceive: hellosend: hello$ node client.jsconnect to serversend: helloreceive: hello
登录后复制

使用 HTTP 协议

因为 HTTP 协议也是基于 TCP 的,所以从通信角度看,这种方式本质上并无区别,只是封装了上层协议。server.js 代码为:

const http = require('http')http.createServer((req, res) => res.end(req.url)).listen(8888)
登录后复制

client.js 代码:

const http = require('http')const options = {  hostname: '192.168.10.105',  port: 8888,  path: '/hello',  method: 'GET',}const req = http.request(options, res => {  console.log(`statusCode: ${res.statusCode}`)  res.on('data', d => process.stdout.write(d))})req.on('error', error => console.error(error))req.end()
登录后复制

运行效果:

$ node server.jsurl /hello$ node client.jsstatusCode: 200hello
登录后复制同一台电脑上两个 Node.js 进程间通信

虽然网络 socket 也可用于同一台主机的进程间通讯(通过 loopback 地址 127.0.0.1),但是这种方式需要经过网络协议栈、需要打包拆包、计算校验和、维护序号和应答等,就是为网络通讯设计的,而同一台电脑上的两个进程可以有更高效的通信方式,即 IPC(Inter-Process Communication),在 unix 上具体的实现方式为 unix domain socket,这是服务器端和客户端之间通过本地打开的套接字文件进行通信的一种方法,与 TCP 通信不同,通信时指定本地文件,因此不进行域解析和外部通信,所以比 TCP 快,在同一台主机的传输速度是 TCP 的两倍。

使用内置 IPC 通道

如果是跟自己创建的子进程通信,是非常方便的,child_process 模块中的 fork 方法自带通信机制,无需关注底层细节,例如父进程 parent.js 代码:

const fork = require("child_process").forkconst path = require("path")const child = fork(path.resolve("child.js"), [], { stdio: "inherit" });child.on("message", (message) => {  console.log("message from child:", message)  child.send("hi")})
登录后复制

子进程 child.js 代码:

process.on("message", (message) => {  console.log("message from parent:", message);})if (process.send) {  setInterval(() => process.send("hello"), 3000)}
登录后复制

运行效果如下:

$ node parent.jsmessage from child: hellomessage from parent: himessage from child: hellomessage from parent: hi
登录后复制

使用自定义管道

如果是两个独立的 Node.js 进程,如何建立通信通道呢?在 Windows 上可以使用命名管道(Named PIPE),在 unix 上可以使用 unix domain socket,也是一个作为 server,另外一个作为 client,其中 server.js 代码如下:

const net = require('net')const fs = require('fs')const pipeFile = process.platform === 'win32' ? '\\\\.\\pipe\\mypip' : '/tmp/unix.sock'const server = net.createServer(connection => {  console.log('socket connected.')  connection.on('close', () => console.log('disconnected.'))  connection.on('data', data => {    console.log(`receive: ${data}`)    connection.write(data)    console.log(`send: ${data}`)  })  connection.on('error', err => console.error(err.message))})try {  fs.unlinkSync(pipeFile)} catch (error) {}server.listen(pipeFile)
登录后复制

client.js 代码如下:

const net = require('net')const client = net.connect(8888, '192.168.10.105')client.on('connect', () => console.log('connected.'))client.on('data', data => console.log(`receive: ${data}`))client.on('end', () => console.log('disconnected.'))client.on('error', err => console.error(err.message))setInterval(() => {  const msg = 'hello'  console.log(`send: ${msg}`)  client.write(msg)}, 3000)0
登录后复制

运行效果:

const net = require('net')const client = net.connect(8888, '192.168.10.105')client.on('connect', () => console.log('connected.'))client.on('data', data => console.log(`receive: ${data}`))client.on('end', () => console.log('disconnected.'))client.on('error', err => console.error(err.message))setInterval(() => {  const msg = 'hello'  console.log(`send: ${msg}`)  client.write(msg)}, 3000)1
登录后复制

更多编程相关知识,请访问:编程入门!!

以上就是分场景讲解两个 Node.js 进程间如何进行通信!的详细内容,更多请关注9543建站博客其它相关文章!

广告:SSL证书一年128.66元起,点击购买~~~

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。

作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:HTML hr是什么意思
下一篇:uniapp同步缓存失效怎么办

发表评论

关闭广告
关闭广告