Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Last active November 11, 2019 14:59
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 mattdesl/3e6c0b23f9f62ac770bef19c63028df8 to your computer and use it in GitHub Desktop.
Save mattdesl/3e6c0b23f9f62ac770bef19c63028df8 to your computer and use it in GitHub Desktop.
const WebSocket = require("ws");
const wss = new WebSocket.Server({ port: 8080 });
let newData;
// ---
// somehow get newData from serialport
// ---
...
// ---
// send to websocket clients
// ---
setInterval(update, 1000 / 30);
function update() {
const obj = {
data: newData
};
const str = JSON.stringify(obj);
const clients = Array.from(wss.clients);
for (let i = 0; i < clients.length; i++) {
const client = clients[i];
client.send(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment