简体   繁体   English

使用 node.js 连接到已经建立的 UNIX 套接字?

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

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.我正在开发一个 node.js 应用程序,该应用程序将连接到 UNIX 套接字(在 Linux 机器上)并促进网页和该套接字之间的通信。 So far, I have been able to create socket and communicate back and forth with this code in my main app.js:到目前为止,我已经能够在我的主 app.js 中创建套接字并与此代码来回通信:

var net = require('net');
var fs = require('fs');
var socketPath = '/tmp/mysocket';

fs.stat(socketPath, function(err) {
    if (!err) fs.unlinkSync(socketPath);
    var unixServer = net.createServer(function(localSerialConnection) {
        localSerialConnection.on('data', function(data) {
            // data is a buffer from the socket
        });
        // write to socket with localSerialConnection.write()
    });

unixServer.listen(socketPath);
});

This code causes node.js to create a UNIX socket at /tmp/mysocket and I am getting good communication by testing with nc -U /tmp/mysocket on the command line.此代码导致 node.js 在/tmp/mysocket创建一个 UNIX 套接字,通过在命令行上使用nc -U /tmp/mysocket进行测试,我获得了良好的通信。 However...不过……

I want to establish a connection to an already existing UNIX socket from my node.js application.我想从我的 node.js 应用程序建立到一个已经存在的 UNIX 套接字的连接。 With my current code, if I create a socket from the command line ( nc -Ul /tmp/mysocket ), then run my node.js application, there is no communication between the socket and my application (The 'connect' event is not fired from node.js server object).使用我当前的代码,如果我从命令行( nc -Ul /tmp/mysocket )创建一个套接字,然后运行我的 node.js 应用程序,套接字和我的应用程序之间没有通信('connect' 事件不是从 node.js 服务器对象触发)。

Any tips on how to go about accomplishing this?有关如何实现这一目标的任何提示? My experiments with node.js function net.createSocket instead of net.createServer have so far failed and I'm not sure if that's even the right track.到目前为止,我使用 node.js 函数net.createSocket而不是net.createServer实验失败了,我不确定这是否是正确的轨道。

The method you're looking for is net.createConnection(path) :您正在寻找的方法是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 ...
});

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.我只是想让它与 Linux 的抽象套接字一起工作,但发现它们与节点的网络库不兼容。 Instead, the following code can be used with the abstract-socket library:相反,以下代码可以与abstract-socket库一起使用:

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 ...
});

You can also connect to a socket like this:您还可以像这样连接到套接字:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM