Last active
January 7, 2019 23:22
-
-
Save robzhu/38c2a673e293561750c54c3898f7f879 to your computer and use it in GitHub Desktop.
WS server refactored to split execution logic from state
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 short = require('short-uuid'); | |
const connections = {}; | |
const send = (connectionId, data) => { | |
const connection = connections[connectionId]; | |
connection.send(data); | |
} | |
const defaultActions = { | |
connect: (connection) => { | |
const id = short.generate(); | |
connection.connectionId = id | |
connections[id] = connection; | |
console.log(`client connected with connectionId: ${id}`); | |
customActions.connect && customActions.connect(id); | |
}, | |
disconnect: (connectionId) => { | |
delete connections[connectionId]; | |
console.log(`client disconnected with connectionId: ${connectionId}`); | |
customActions.disconnect && customActions.disconnect(connectionId); | |
}, | |
default: (connectionId, message) => { | |
customActions.default ? customActions.default(connectionId) : | |
send(connectionId, message ? `unrecognized action: ${message.action}` | |
: `message cannot be empty`) | |
}, | |
}; | |
const customActions = { | |
echo: (connectionId, data) => { | |
send(connectionId, data); | |
} | |
}; | |
const wss = new WebSocket.Server({ port: 8080 }); | |
wss.on('connection', socket => { | |
defaultActions.connect(socket); | |
socket.on('message', messageJson => { | |
console.log(`Received: ${messageJson}`); | |
try { | |
const { action, data } = JSON.parse(messageJson); | |
// call the matching custom handler, else call the default handler | |
const customHandler = customActions[action]; | |
customHandler ? customHandler(socket.connectionId, data) : | |
defaultActions.default(socket.connectionId, { action, data }); | |
} catch (ex) { | |
console.error(ex); | |
socket.send(`Bad Request format, use: '{"action": ..., "data": ...}'`); | |
} | |
}); | |
socket.on('close', () => { | |
defaultActions.disconnect(socket.connectionId); | |
}); | |
}); | |
console.log(`Listening on ws://localhost:8080`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment