-
-
Save Happy-Ferret/7e060accd46c0840df0eae18cf9fef02 to your computer and use it in GitHub Desktop.
Passing messages between Node.js and C#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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