Created
November 21, 2022 17:40
-
-
Save yakovenkodenis/262f2adb7d9fc592967d3360cbbbd282 to your computer and use it in GitHub Desktop.
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 PORT = 4000; | |
const server = new WebSocketServer({ port: PORT }); | |
server.on('data', async (message, reply) => { | |
if (!message) return; | |
const data = JSON.parse(message); | |
const { method, args = [] } = data; | |
const handler = api[method]; | |
if (!handler) return reply({ error: 'Not Found' }); | |
try { | |
const result = await handler(...args); | |
reply(result); | |
} catch (error) { | |
console.error(error); | |
reply({ error: 'Internal Server Error' }); | |
} | |
}); | |
server.listen(() => { | |
console.log(`WebSocket server listening on port ${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment