Skip to content

Instantly share code, notes, and snippets.

@yakovenkodenis
Created November 21, 2022 17:40
Show Gist options
  • Save yakovenkodenis/262f2adb7d9fc592967d3360cbbbd282 to your computer and use it in GitHub Desktop.
Save yakovenkodenis/262f2adb7d9fc592967d3360cbbbd282 to your computer and use it in GitHub Desktop.
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