Created
February 5, 2024 22:13
-
-
Save ruzli/2ffa2639ac9eff30acd1c0283b5b2fe6 to your computer and use it in GitHub Desktop.
wss2.js
This file contains hidden or 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 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