Last active
July 2, 2018 12:44
-
-
Save PierreAndreis/8d192914ecb3b95de3ff6ef4ef1c34d6 to your computer and use it in GitHub Desktop.
This file contains 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
// This example depends on hello.js to be running in another process. | |
// This Node is a socket that replies to hello.js with "world!" when it receives "Hello". | |
// This example tests concurrency with parallel messages in IPC. | |
const { Node } = require("../src/index"); | |
const node = new Node("concurrent") | |
.on("error", console.error) | |
.on("connect", () => console.log("Connected!")); | |
node | |
.connectTo("hello", 8002) | |
.then(socket => | |
Promise.all( | |
Array.from({ length: 50 }, (_, i) => { | |
// 10 seconds timeout | |
let timeout = setTimeout( | |
() => console.log(`timeout for test ${i}`), | |
10000 | |
); | |
node.sendTo(socket, `Test ${i}`, 1).then(reply => { | |
console.log(`GOT IT for ${i}`, reply); | |
clearTimeout(timeout); | |
}); | |
}) | |
) | |
) | |
.catch(() => console.log("Disconnected!")); |
This file contains 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
// This example must be run before interactive/world, since this serves the | |
// IPC server the other sockets connect to. | |
const { Node } = require("../src/index"); | |
const node = new Node("hello") | |
.on("connection", (name, socket) => { | |
console.log(`Connected to ${name}`); | |
}) | |
.on("listening", console.log.bind(null, "Listening")) | |
.on("message", message => { | |
console.log(`Received data:`, message); | |
setTimeout( | |
() => message.reply(`Reply!: ${message.data}`), | |
Math.floor(Math.random()) * 1000 | |
); | |
}) | |
.on("error", console.error.bind(null, "Error")) | |
.on("socketClose", console.log.bind(null, "Closed Socket:")) | |
.serve("hello", 8002); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment