Created
July 8, 2018 04:08
-
-
Save oldmud0/bf78f27525f05faa07b8a9ef981f1f03 to your computer and use it in GitHub Desktop.
node-ipc example with high client->server reliability (no dropped messages)
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
const ipc = require("node-ipc"); | |
const async = require("async"); | |
const queue = async.queue((data, cb) => { | |
// There is no way of checking if client is connected so just | |
// catch and put it back in the queue if a bad happens. | |
try { | |
ipc.of.test.emit("message", data); | |
} catch (err) { | |
console.error(err); | |
queue.unshift(data); | |
} | |
cb(); | |
}); | |
queue.pause(); | |
let i = 0; | |
ipc.config.id = "test"; | |
ipc.config.silent = true; | |
setInterval(() => { | |
console.log(`Buffered message ${i}`); | |
queue.push(i++); | |
}, 500); | |
ipc.connectToNet("test", () => { | |
ipc.of.test.on("connect", () => { | |
console.log("connected"); | |
queue.resume(); | |
}); | |
ipc.of.test.on("message", (data) => { | |
console.log(data); | |
}); | |
ipc.of.test.on("error", (err) => { | |
queue.pause(); | |
}) | |
}); |
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
const ipc = require("node-ipc"); | |
ipc.config.id = "test"; | |
ipc.serveNet(() => { | |
ipc.server.on("connect", () => { | |
ipc.server.broadcast("message", "yoyooyoyoy"); | |
}); | |
ipc.server.on("message", (data, socket) => { | |
ipc.server.emit(socket, "message", data); | |
}); | |
}); | |
ipc.server.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment