如何在 node.js 中创建命名管道?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11750041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:07:16  来源:igfitidea点击:

How to create a named pipe in node.js?

node.jspipenamed-pipes

提问by wako

How to create a named pipe in node.js?

如何在 node.js 中创建命名管道?

P.S.: For now I'm creating a named pipe as follows. But I think this is not best way

PS:现在我正在创建一个命名管道,如下所示。但我认为这不是最好的方法

var mkfifoProcess = spawn('mkfifo',  [fifoFilePath]);
mkfifoProcess.on('exit', function (code) {
    if (code == 0) {
        console.log('fifo created: ' + fifoFilePath);
    } else {
        console.log('fail to create fifo with code:  ' + code);
    }
});

回答by befzz

Working with named pipes on Windows

在 Windows 上使用命名管道

Node v0.12.4

节点 v0.12.4

var net = require('net');

var PIPE_NAME = "mypipe";
var PIPE_PATH = "\\.\pipe\" + PIPE_NAME;

var L = console.log;

var server = net.createServer(function(stream) {
    L('Server: on connection')

    stream.on('data', function(c) {
        L('Server: on data:', c.toString());
    });

    stream.on('end', function() {
        L('Server: on end')
        server.close();
    });

    stream.write('Take it easy!');
});

server.on('close',function(){
    L('Server: on close');
})

server.listen(PIPE_PATH,function(){
    L('Server: on listening');
})

// == Client part == //
var client = net.connect(PIPE_PATH, function() {
    L('Client: on connection');
})

client.on('data', function(data) {
    L('Client: on data:', data.toString());
    client.end('Thanks!');
});

client.on('end', function() {
    L('Client: on end');
})

Output:

输出:

Server: on listening
Client: on connection
Server: on connection
Client: on data: Take it easy!
Server: on data: Thanks!
Client: on end
Server: on end
Server: on close

Note about pipe names:

关于管道名称的注意事项:

C/C++ / Nodejs:
\\.\pipe\PIPENAMECreateNamedPipe

.Net / Powershell:
\\.\PIPENAMENamedPipeClientStream / NamedPipeServerStream

Both will use file handle:
\Device\NamedPipe\PIPENAME

C/C++ / Nodejs:
\\.\pipe\PIPENAMECreateNamedPipe

.Net / Powershell:
\\.\PIPENAMENamedPipeClientStream / NamedPipeServerStream

两者都将使用文件句柄:
\Device\NamedPipe\PIPENAME

回答by jpillora

Looks like name pipes aren't and won't be supported in Node core - from Ben Noordhuis 10/11/11:

看起来 Node 核心不支持也不支持名称管道 - 来自 Ben Noordhuis 10/11/11:

Windows has a concept of named pipes but since you mention mkfifoI assume you mean UNIX FIFOs.

We don't support them and probably never will (FIFOs in non-blocking mode have the potential to deadlock the event loop) but you can use UNIX sockets if you need similar functionality.

Windows 有命名管道的概念,但既然你提到了,mkfifo我假设你的意思是 UNIX FIFO。

我们不支持它们并且可能永远不会支持(非阻塞模式下的 FIFO 有可能使事件循环死锁),但是如果您需要类似的功能,您可以使用 UNIX 套接字。

https://groups.google.com/d/msg/nodejs/9TvDwCWaB5c/udQPigFvmgAJ

https://groups.google.com/d/msg/nodejs/9TvDwCWaB5c/udQPigFvmgAJ

Named pipes and sockets are very similar however, the netmodule implements local sockets by specifying a pathas opposed to a hostand port:

命名管道和套接字非常相似,但是该net模块通过指定 apath而不是 a hostand来实现本地套接字port

Example:

例子:

var net = require('net');

var server = net.createServer(function(stream) {
  stream.on('data', function(c) {
    console.log('data:', c.toString());
  });
  stream.on('end', function() {
    server.close();
  });
});

server.listen('/tmp/test.sock');

var stream = net.connect('/tmp/test.sock');
stream.write('hello');
stream.end();

回答by Vodun

Maybe use fs.watchFileinstead of named pipe ? See documentation

也许使用fs.watchFile而不是命名管道?查看文档