[Edit]
+
0
-
0

Node.js - create named pipe under Windows

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
// ------------------------------------------------- // server.js file: // ------------------------------------------------- const net = require('net'); const server = net.createServer((stream) => { // called on client connected stream.on('data', function(data) { // called on client message console.log(data.toString()); // prints client message }); stream.on('end', () => { // called on client disconnected server.close(); // stops pipe server }); stream.write('Hi Client!'); // sends text to client }); server.on('close', () => { // called on server stopped // ... }); const pipe = "\\\\.\\pipe\\my-pipe"; // equivalent to Unix /tmp/my-pipe.sock server.listen(pipe, () => { // called on server started // ... }); // ------------------------------------------------- // client.js file: // ------------------------------------------------- const net = require('net'); const pipe = "\\\\.\\pipe\\my-pipe"; // equivalent to Unix /tmp/my-pipe.sock const client = net.connect(pipe, () => { // called on client connected // ... }); client.on('data', (data) => { console.log(data.toString()); // prints server message client.write('Hi Server!'); // sends text to server client.end(); // disconnects client }); client.on('end', () => { // called on client disconnected // ... }); // See also: // // 1. https://dirask.com/snippets/Node-js-create-named-pipe-under-Windows-pJaoYD