最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解兩個Node.js進(jìn)程是如何通信

 更新時間:2021年10月09日 10:43:30   作者:喬珂力  
進(jìn)程間通信是是Node.js的一個十分重要的部分,這篇文章主要給大家介紹了關(guān)于兩個Node.js進(jìn)程是如何通信的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

兩個 Node.js 進(jìn)程之間如何進(jìn)行通信呢?這里要分兩種場景:

  1. 不同電腦上的兩個 Node.js 進(jìn)程間通信
  2. 同一臺電腦上兩個 Node.js 進(jìn)程間通信

對于第一種場景,通常使用 TCP 或 HTTP 進(jìn)行通信,而對于第二種場景,又分為兩種子場景:

  1. Node.js 進(jìn)程和自己創(chuàng)建的 Node.js 子進(jìn)程通信
  2. Node.js 進(jìn)程和另外不相關(guān)的 Node.js 進(jìn)程通信

前者可以使用內(nèi)置的 IPC 通信通道,后者可以使用自定義管道,接下來進(jìn)行詳細(xì)介紹:

不同電腦上的兩個 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ìn)程進(jìn)行通信。一個作為 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)

運(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

同一臺電腦上兩個 Node.js 進(jìn)程間通信

雖然網(wǎng)絡(luò) socket 也可用于同一臺主機(jī)的進(jìn)程間通訊(通過 loopback 地址 127.0.0.1),但是這種方式需要經(jīng)過網(wǎng)絡(luò)協(xié)議棧、需要打包拆包、計算校驗(yàn)和、維護(hù)序號和應(yīng)答等,就是為網(wǎng)絡(luò)通訊設(shè)計的,而同一臺電腦上的兩個進(jìn)程可以有更高效的通信方式,即 IPC(Inter-Process Communication),在 unix 上具體的實(shí)現(xiàn)方式為 unix domain socket,這是服務(wù)器端和客戶端之間通過本地打開的套接字文件進(jìn)行通信的一種方法,與 TCP 通信不同,通信時指定本地文件,因此不進(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

使用自定義管道

如果是兩個獨(dú)立的 Node.js 進(jìn)程,如何建立通信通道呢?在 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)

運(yùn)行效果:

$ node server.js 
socket connected.
receive: hello
send: hello

$ node client.js
connected.
send: hello
receive: hello

總結(jié)

到此這篇關(guān)于兩個Node.js進(jìn)程是如何通信的文章就介紹到這了,更多相關(guān)兩個Node.js進(jìn)程通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

莱州市| 郓城县| 唐河县| 浦北县| 伊宁市| 吉林市| 邵阳县| 英山县| 龙州县| 西林县| 岳阳市| 台州市| 奉化市| 陵川县| 车险| 化德县| 安远县| 蒙阴县| 尉犁县| 沁阳市| 大厂| 竹北市| 金寨县| 那曲县| 佛教| 吴堡县| 鄢陵县| 江安县| 阜阳市| 河东区| 冕宁县| 荆门市| 时尚| 嘉黎县| 额尔古纳市| 滨州市| 高碑店市| 宁河县| 新巴尔虎右旗| 龙泉市| 溆浦县|