如何在node.js中创建命名管道? [英] How to create a named pipe in node.js?
问题描述
如何在node.js中创建命名管道?
How to create a named pipe in node.js?
P.S .: 现在,我将如下创建一个命名管道.但是我认为这不是最好的方法
P.S.: For now I'm creating a named pipe as follows. But I think this is not best way
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);
}
});
推荐答案
Node核心不支持名称管道,例如Ben Noordhuis 10/11/11:
Looks like name pipes aren't and won't be supported in Node core - from Ben Noordhuis 10/11/11:
Windows具有命名管道的概念,但是自从您提到
mkfifo
以来,我 假设您是指UNIX FIFO.
Windows has a concept of named pipes but since you mention
mkfifo
I assume you mean UNIX FIFOs.
我们不支持它们,可能永远也不会(非阻塞中的FIFO 模式有可能死锁事件循环),但您可以使用 如果需要类似的功能,请使用UNIX套接字.
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.
https://groups.google.com/d/msg/nodejs/9TvDwCWaB5c/udQPigFvmgAJ
命名管道和套接字非常相似,但是net
模块通过指定path
而不是host
和port
来实现本地套接字:
Named pipes and sockets are very similar however, the net
module implements local sockets by specifying a path
as opposed to a host
and port
:
- http://nodejs.org/api/net.html#net_server_listen_path_callback
- http://nodejs.org/api/net.html#net_net_connect_path_connectlistener
- http://nodejs.org/api/net.html#net_server_listen_path_callback
- http://nodejs.org/api/net.html#net_net_connect_path_connectlistener
示例:
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();
这篇关于如何在node.js中创建命名管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!