Skip to content

Instantly share code, notes, and snippets.

@e4coder
Forked from kevinswiber/Program.cs
Created May 21, 2021 21:24
Show Gist options
  • Save e4coder/2dd824a6e5dd13d4fef58d7036f16e46 to your computer and use it in GitHub Desktop.
Save e4coder/2dd824a6e5dd13d4fef58d7036f16e46 to your computer and use it in GitHub Desktop.
Passing messages between Node.js and C#.
using System;
using System.Text;
namespace NodeIPC
{
class Program
{
static void Main(string[] args)
{
var input = Console.OpenStandardInput();
var buffer = new byte[1024];
int length;
while (input.CanRead && (length = input.Read(buffer, 0, buffer.Length)) > 0)
{
var payload = new byte[length];
Buffer.BlockCopy(buffer, 0, payload, 0, length);
Console.Write("Receiving: " + Encoding.UTF8.GetString(payload));
Console.Out.Flush();
}
}
}
}
// run `node server.js` with NodeIPC.exe in the same directory.
var spawn = require('child_process').spawn;
var ipc = spawn("./NodeIPC.exe");
ipc.stdin.setEncoding("utf8");
ipc.stderr.on('data', function (data) {
process.stdout.write(data.toString());
});
ipc.stdout.on('data', function (data) {
process.stdout.write(data.toString());
});
var message = { type: "Greeting", data: "Hello, World!" };
console.log("Sending: ", JSON.stringify(message));
ipc.stdin.write(JSON.stringify(message) + "\n");
// to allow sending messages by typing into the console window.
var stdin = process.openStdin();
stdin.on('data', function (data) {
ipc.stdin.write(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment