How to create a named pipe in node.js?

后端 未结 3 1677
暖寄归人
暖寄归人 2020-12-04 15:30

How to create a named pipe in node.js?

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

var mkfifoProcess = spa         


        
3条回答
  •  误落风尘
    2020-12-04 16:34

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

    Windows has a concept of named pipes but since you mention mkfifo I 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.

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

    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

    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();
    

提交回复
热议问题