詳解兩個Node.js進程是如何通信
前言
兩個 Node.js 進程之間如何進行通信呢?這里要分兩種場景:
- 不同電腦上的兩個 Node.js 進程間通信
- 同一臺電腦上兩個 Node.js 進程間通信
對于第一種場景,通常使用 TCP 或 HTTP 進行通信,而對于第二種場景,又分為兩種子場景:
- Node.js 進程和自己創(chuàng)建的 Node.js 子進程通信
- Node.js 進程和另外不相關的 Node.js 進程通信
前者可以使用內(nèi)置的 IPC 通信通道,后者可以使用自定義管道,接下來進行詳細介紹:
不同電腦上的兩個 Node.js 進程間通信
要想進行通信,首先得搞清楚如何標識網(wǎng)絡中的進程?網(wǎng)絡層的 ip 地址可以唯一標識網(wǎng)絡中的主機,而傳輸層的協(xié)議和端口可以唯一標識主機中的應用程序(進程),這樣利用三元組(ip 地址,協(xié)議,端口)就可以標識網(wǎng)絡的進程了。
使用 TCP 套接字
TCP 套接字(socket)是一種基于 TCP/IP 協(xié)議的通信方式,可以讓通過網(wǎng)絡連接的計算機上的進程進行通信。一個作為 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.js client connected receive: hello send: hello $ node client.js connect to server send: hello receive: hello
使用 HTTP 協(xié)議
因為 HTTP 協(xié)議也是基于 TCP 的,所以從通信角度看,這種方式本質(zhì)上并無區(qū)別,只是封裝了上層協(xié)議。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.js url /hello $ node client.js statusCode: 200 hello
同一臺電腦上兩個 Node.js 進程間通信
雖然網(wǎng)絡 socket 也可用于同一臺主機的進程間通訊(通過 loopback 地址 127.0.0.1),但是這種方式需要經(jīng)過網(wǎng)絡協(xié)議棧、需要打包拆包、計算校驗和、維護序號和應答等,就是為網(wǎng)絡通訊設計的,而同一臺電腦上的兩個進程可以有更高效的通信方式,即 IPC(Inter-Process Communication),在 unix 上具體的實現(xiàn)方式為 unix domain socket,這是服務器端和客戶端之間通過本地打開的套接字文件進行通信的一種方法,與 TCP 通信不同,通信時指定本地文件,因此不進行域解析和外部通信,所以比 TCP 快,在同一臺主機的傳輸速度是 TCP 的兩倍。
使用內(nèi)置 IPC 通道
如果是跟自己創(chuàng)建的子進程通信,是非常方便的,child_process 模塊中的 fork 方法自帶通信機制,無需關注底層細節(jié),例如父進程 parent.js 代碼:
const fork = require("child_process").fork
const 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.js message from child: hello message from parent: hi message from child: hello message 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 pipeFile = process.platform === 'win32' ? '\\\\.\\pipe\\mypip' : '/tmp/unix.sock'
const client = net.connect(pipeFile)
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.js socket connected. receive: hello send: hello $ node client.js connected. send: hello receive: hello
總結(jié)
到此這篇關于兩個Node.js進程是如何通信的文章就介紹到這了,更多相關兩個Node.js進程通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解如何在Node.js的httpServer中接收前端發(fā)送的arraybuffer數(shù)據(jù)
這篇文章主要介紹了詳解如何在Node.js的httpServer中接收前端發(fā)送的arraybuffer數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
node.js中stream流中可讀流和可寫流的實現(xiàn)與使用方法實例分析
這篇文章主要介紹了node.js中stream流中可讀流和可寫流的實現(xiàn)與使用方法,結(jié)合實例形式分析了node.js stream流可讀流和可寫流基本分類、原理、定義、使用方法及相關注意事項,需要的朋友可以參考下2020-02-02

