Skip to content

Instantly share code, notes, and snippets.

@ruzli
Created February 5, 2024 22:13
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 ruzli/2ffa2639ac9eff30acd1c0283b5b2fe6 to your computer and use it in GitHub Desktop.
Save ruzli/2ffa2639ac9eff30acd1c0283b5b2fe6 to your computer and use it in GitHub Desktop.
wss2.js
const WebSocket = require('ws');
const readline = require('readline');
const wss = new WebSocket.Server({ port: 4041 });
wss.on('connection', (ws) => {
console.log(getCurrentTime() + 'Connected');
ws.on('close', () => {
console.log(getCurrentTime() + 'Disconnected');
});
});
function getCurrentTime() {
return "[" + String((new Date()).toLocaleTimeString('en-US', { timeZone: 'Europe/Moscow' })) + "] ";
}
console.log('Started ' + getCurrentTime());
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', (key) => {
if (key === '\u0003') {
process.exit();
}
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ hotkey: key }));
}
});
console.log(getCurrentTime() + `"${key.toString().toUpperCase()}" was pressed.`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment