詳解兩個(gè)Node.js進(jìn)程是如何通信
前言
兩個(gè) Node.js 進(jìn)程之間如何進(jìn)行通信呢?這里要分兩種場景:
- 不同電腦上的兩個(gè) Node.js 進(jìn)程間通信
- 同一臺電腦上兩個(gè) Node.js 進(jìn)程間通信
對于第一種場景,通常使用 TCP 或 HTTP 進(jìn)行通信,而對于第二種場景,又分為兩種子場景:
- Node.js 進(jìn)程和自己創(chuàng)建的 Node.js 子進(jìn)程通信
- Node.js 進(jìn)程和另外不相關(guān)的 Node.js 進(jìn)程通信
前者可以使用內(nèi)置的 IPC 通信通道,后者可以使用自定義管道,接下來進(jìn)行詳細(xì)介紹:
不同電腦上的兩個(gè) Node.js 進(jìn)程間通信
要想進(jìn)行通信,首先得搞清楚如何標(biāo)識網(wǎng)絡(luò)中的進(jìn)程?網(wǎng)絡(luò)層的 ip 地址可以唯一標(biāo)識網(wǎng)絡(luò)中的主機(jī),而傳輸層的協(xié)議和端口可以唯一標(biāo)識主機(jī)中的應(yīng)用程序(進(jìn)程),這樣利用三元組(ip 地址,協(xié)議,端口)就可以標(biāo)識網(wǎng)絡(luò)的進(jìn)程了。
使用 TCP 套接字
TCP 套接字(socket)是一種基于 TCP/IP 協(xié)議的通信方式,可以讓通過網(wǎng)絡(luò)連接的計(jì)算機(jī)上的進(jìn)程進(jìn)行通信。一個(gè)作為 server 另一個(gè)作為 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)
運(yùn)行效果:
$ node server.js client connected receive: hello send: hello $ node client.js connect to server send: hello receive: hello
使用 HTTP 協(xié)議
因?yàn)?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()
運(yùn)行效果:
$ node server.js url /hello $ node client.js statusCode: 200 hello
同一臺電腦上兩個(gè) Node.js 進(jìn)程間通信
雖然網(wǎng)絡(luò) socket 也可用于同一臺主機(jī)的進(jìn)程間通訊(通過 loopback 地址 127.0.0.1),但是這種方式需要經(jīng)過網(wǎng)絡(luò)協(xié)議棧、需要打包拆包、計(jì)算校驗(yàn)和、維護(hù)序號和應(yīng)答等,就是為網(wǎng)絡(luò)通訊設(shè)計(jì)的,而同一臺電腦上的兩個(gè)進(jìn)程可以有更高效的通信方式,即 IPC(Inter-Process Communication),在 unix 上具體的實(shí)現(xiàn)方式為 unix domain socket,這是服務(wù)器端和客戶端之間通過本地打開的套接字文件進(jìn)行通信的一種方法,與 TCP 通信不同,通信時(shí)指定本地文件,因此不進(jìn)行域解析和外部通信,所以比 TCP 快,在同一臺主機(jī)的傳輸速度是 TCP 的兩倍。
使用內(nèi)置 IPC 通道
如果是跟自己創(chuàng)建的子進(jìn)程通信,是非常方便的,child_process 模塊中的 fork 方法自帶通信機(jī)制,無需關(guān)注底層細(xì)節(jié),例如父進(jìn)程 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") })
子進(jìn)程 child.js 代碼:
process.on("message", (message) => { console.log("message from parent:", message); }) if (process.send) { setInterval(() => process.send("hello"), 3000) }
運(yùn)行效果如下:
$ node parent.js message from child: hello message from parent: hi message from child: hello message from parent: hi
使用自定義管道
如果是兩個(gè)獨(dú)立的 Node.js 進(jìn)程,如何建立通信通道呢?在 Windows 上可以使用命名管道(Named PIPE),在 unix 上可以使用 unix domain socket,也是一個(gè)作為 server,另外一個(gè)作為 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)
運(yùn)行效果:
$ node server.js socket connected. receive: hello send: hello $ node client.js connected. send: hello receive: hello
總結(jié)
到此這篇關(guān)于兩個(gè)Node.js進(jìn)程是如何通信的文章就介紹到這了,更多相關(guān)兩個(gè)Node.js進(jìn)程通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
NPM 安裝cordova時(shí)警告:npm WARN deprecated minimatch@2.0.10: Pleas
這篇文章主要介紹了NPM 安裝cordova時(shí)警告:npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to的相關(guān)資料,需要的朋友可以參考下2016-12-12Node.js+jade抓取博客所有文章生成靜態(tài)html文件的實(shí)例
下面小編就為大家?guī)硪黄狽ode.js+jade抓取博客所有文章生成靜態(tài)html文件的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09Nodejs實(shí)現(xiàn)的操作MongoDB數(shù)據(jù)庫功能完整示例
這篇文章主要介紹了Nodejs實(shí)現(xiàn)的操作MongoDB數(shù)據(jù)庫功能,結(jié)合完整實(shí)例形式分析了nodejs針對MongoDB數(shù)據(jù)庫的連接及增刪改查基本操作技巧,需要的朋友可以參考下2019-02-02學(xué)習(xí)使用ExpressJS 4.0中的新Router的用法
ExpressJS 4.0中提出了新的路由Router,提供了路由應(yīng)有的API,本文詳細(xì)的介紹了ExpressJS 4.0中的新Router的用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-11-11node.js實(shí)現(xiàn)復(fù)制文本到剪切板的功能
這篇文章主要給大家介紹了node.js實(shí)現(xiàn)復(fù)制文本到剪切板的功能,文中介紹的非常詳細(xì),并給出示例代碼,相信對大家具有一定的參考借鑒價(jià)值,有需要的朋友們下面來一起看看吧。2017-01-01