javascript 使用 node.js 连接到已经建立的 UNIX 套接字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20807900/
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
Connecting to an already established UNIX socket with node.js?
提问by AnalogWeapon
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 been able to create socket and communicate back and forth with this code in my main app.js:
我正在开发一个 node.js 应用程序,该应用程序将连接到 UNIX 套接字(在 Linux 机器上)并促进网页和该套接字之间的通信。到目前为止,我已经能够在我的主 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. However...
这段代码导致 node.js 在 at 创建一个 UNIX 套接字,/tmp/mysocket
我通过nc -U /tmp/mysocket
在命令行上测试获得了良好的通信。然而...
I want to establish a connection to an already existing UNIX socket from my node.js application. 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).
我想从我的 node.js 应用程序建立到一个已经存在的 UNIX 套接字的连接。使用我当前的代码,如果我从命令行 ( 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
到目前为止的实验失败了,我不确定这是否是正确的轨道。
回答by duskwuff -inactive-
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 ...
});
回答by CompileYourCake
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:
我只是想让它与 Linux 的抽象套接字一起工作,但发现它们与节点的网络库不兼容。相反,可以将以下代码与abstract-socket
库一起使用:
const abstract_socket = require('abstract-socket');
let client = abstract_socket.connect('##代码##my_abstract_socket');
client.on("connect", function() {
... do something when you connect ...
});
client.on("data", function(data) {
... do stuff with the data ...
});
回答by nikallass
You can also connect to a socket like this:
您还可以像这样连接到套接字: