Connecting to an already established UNIX socket with node.js?

后端 未结 3 1116
一整个雨季
一整个雨季 2021-01-02 04:33

I am working on a node.js application that will connect to a UNIX socket (on a Linux machine) and facilitate communication between a web page and that socket. So far, I have

相关标签:
3条回答
  • 2021-01-02 04:57

    You can also connect to a socket like this:

    http://unix:/path/to/my.sock:

    0 讨论(0)
  • 2021-01-02 05:05

    The method you're looking for is net.createConnection(path):

    var client = net.createConnection("/tmp/mysocket");
    
    client.on("connect", function() {
        ... do something when you connect ...
    });
    
    client.on("data", function(data) {
        ... do stuff with the data ...
    });
    
    0 讨论(0)
  • 2021-01-02 05:13

    I was just trying to get this to work with Linux's abstract sockets and found them to be incompatible with node's net library. Instead, the following code can be used with the abstract-socket library:

    const abstract_socket = require('abstract-socket');
    
    let client = abstract_socket.connect('\0my_abstract_socket');
    
    client.on("connect", function() {
        ... do something when you connect ...
    });
    
    client.on("data", function(data) {
        ... do stuff with the data ...
    });
    
    0 讨论(0)
提交回复
热议问题