Nodejs与.net进程通信?

Nodejs与.net进程通信?

使用nodejs来启动一个.nect的程序,要实它们之间的进程通信,用什么方案比较好?比如IPC,或者socket…

7 回复

使用nodejs来启动一个.nect的程序,要实它们之间的进程通信,用什么方案比较好?比如IPC,或者socket…


Edge.js:让.NET和Node.js代码比翼齐飞 http://www.cnblogs.com/shanyou/p/3325249.html

Node.js 与 .NET 进程通信

在开发过程中,我们经常需要让不同的进程或应用程序之间进行通信。例如,你可能希望使用 Node.js 启动一个 .NET 程序,并实现两者之间的进程通信。以下是几种常见的方法,包括 IPC(进程间通信) 和 Socket 通信。

1. 使用 IPC (Inter-Process Communication)

Node.js 端

首先,在 Node.js 中使用 child_process 模块来创建子进程并进行 IPC 通信。

const { fork } = require('child_process');

// 创建子进程
const child = fork('./dotnetApp.js');

// 发送消息给子进程
child.send({ type: 'start', data: 'Hello from Node.js' });

// 监听子进程的消息
child.on('message', (msg) => {
    console.log(`Received message from .NET app: ${msg.data}`);
});

.NET 端

为了在 .NET 中处理 IPC 通信,可以使用 System.Diagnostics.Process 类来监听 Node.js 发送的消息。

using System;
using System.Diagnostics;

class Program
{
    static void Main()
    {
        var process = new Process();
        process.StartInfo.FileName = "node";
        process.StartInfo.Arguments = "./nodeApp.js";
        process.Start();

        process.OutputDataReceived += (sender, e) => 
            Console.WriteLine($"Node.js says: {e.Data}");

        process.BeginOutputReadLine();
    }
}

2. 使用 Socket 通信

如果你希望在不同机器或网络中进行通信,可以考虑使用 Socket 通信。

Node.js 端

const net = require('net');

const server = net.createServer((socket) => {
    socket.on('data', (data) => {
        console.log(`Received message from .NET app: ${data.toString()}`);
    });
});

server.listen(6000, () => {
    console.log('Server is listening on port 6000');
});

.NET 端

using System.Net.Sockets;
using System.Text;

class Program
{
    static void Main()
    {
        using (var client = new TcpClient("localhost", 6000))
        {
            using (var stream = client.GetStream())
            {
                byte[] message = Encoding.ASCII.GetBytes("Hello from .NET app");
                stream.Write(message, 0, message.Length);
            }
        }
    }
}

总结

选择哪种方法取决于你的具体需求。如果通信发生在同一台机器上,IPC 可能更简单且高效。如果需要跨网络通信,Socket 会是更好的选择。希望这些示例代码能帮助你实现 Node.js 与 .NET 之间的进程通信。

谢谢!

跟php呢,怎么通信?

要在Node.js和.NET应用程序之间实现进程间通信(IPC),你可以选择多种方法,包括使用命名管道、Socket通信、HTTP服务等。这里我将介绍两种常见的方法:命名管道(Windows)和Socket通信。

方法1: 使用命名管道 (适用于Windows)

Node.js端

首先,你需要安装node-ipc库,它支持多种通信机制:

npm install node-ipc --save

然后可以使用以下代码:

const ipc = require('node-ipc');

// 配置管道名称
ipc.config.id = 'nodeServer';
ipc.config.retry = 1500;
ipc.config.silent = true;

// 启动服务器
ipc.server.start(() => {
    console.log('Server started');
    
    // 接收来自.NET应用的消息
    ipc.server.on('message', (data, socket) => {
        console.log('Received:', data);
        // 可以发送响应消息
        ipc.server.emit(socket, 'response', { message: 'Hello from Node.js' });
    });
});

.NET端

.NET 端可以使用 System.IO.Pipes 来实现相同功能:

using System;
using System.IO.Pipes;
using System.Text;

class Program
{
    static void Main()
    {
        using (var pipeClient = new NamedPipeClientStream(".", "nodeServer", PipeDirection.InOut))
        {
            pipeClient.Connect();
            
            using (var writer = new StreamWriter(pipeClient) { AutoFlush = true })
            using (var reader = new StreamReader(pipeClient))
            {
                writer.WriteLine("Hello from .NET");
                Console.WriteLine(reader.ReadLine());
            }
        }
    }
}

方法2: 使用Socket通信

Node.js端

const net = require('net');

const server = net.createServer((socket) => {
    console.log('Client connected');
    
    socket.on('data', (data) => {
        console.log('Received:', data.toString());
        socket.write(`Response from Node.js: ${data.toString()}`);
    });

    socket.on('end', () => {
        console.log('Client disconnected');
    });
});

server.listen(3000, () => {
    console.log('Server is listening on port 3000');
});

.NET端

using System.Net.Sockets;
using System.Text;

class Program
{
    static void Main()
    {
        using (var client = new TcpClient("localhost", 3000))
        {
            using (var stream = client.GetStream())
            {
                var writer = new StreamWriter(stream) { AutoFlush = true };
                var reader = new StreamReader(stream);

                writer.WriteLine("Hello from .NET");
                Console.WriteLine("Response from Node.js: " + reader.ReadLine());
            }
        }
    }
}

这两种方法都可以有效地实现Node.js与.NET应用程序之间的通信。选择哪种方法取决于你的具体需求和环境。

回到顶部