Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created June 27, 2017 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/b03a91a38de6c438efcbecfd739c1b11 to your computer and use it in GitHub Desktop.
Save rwaldron/b03a91a38de6c438efcbecfd739c1b11 to your computer and use it in GitHub Desktop.
"use strict";
const net = require("net");
const monitor = {
connection: null,
queue: [],
write() {
if (monitor.connection === null) {
monitor.queue.push(arguments);
} else {
monitor.connection.write.apply(monitor.connection, arguments);
}
}
};
const server = net.createServer((c) => {
monitor.connection = c;
c.on("end", () => {
monitor.connection = null;
});
if (monitor.queue.length) {
let message;
while (message = monitor.queue.shift()) {
monitor.connection.write.apply(monitor.connection, message);
}
}
c.pipe(c);
});
server.listen(8124);
// Write will queue all messages and send them
// once a remote client connection is made.
// Once connected, new messages will be sent directly
monitor.write("Hi!");
const net = require("net");
const client = net.createConnection({ host: "your-tessel-name.local", port: 8124 }, () => {
console.log("Connected to Tessel...");
});
client.on("data", (data) => {
console.log(data.toString());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment