最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Connecting to an already established UNIX socket with node.js? - Stack Overflow

programmeradmin3浏览0评论

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:

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...

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

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.

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:

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...

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

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.

Share Improve this question asked Dec 27, 2013 at 21:13 AnalogWeaponAnalogWeapon 5481 gold badge4 silver badges16 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 18

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

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

You can also connect to a socket like this:

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

发布评论

评论列表(0)

  1. 暂无评论