@hp4k1h5/terminordle
Version:
> multiplayer [wordle](https://www.powerlanguage.co.uk/wordle/) clone in your terminal
23 lines (18 loc) • 467 B
text/typescript
import { WS, ClientMessage, ServerMessage, MsgType } from '../../lib/structs'
export function msg(cnx: WS, m: ServerMessage) {
cnx.send(JSON.stringify(m))
}
export function validateMsg(
cnx: WS,
data: string | ClientMessage,
): ClientMessage {
try {
data = JSON.parse(data as string) as ClientMessage
} catch (e) {
throw 'bad json'
}
if (!data.type || !(data.type in MsgType)) {
throw `bad message type ${data.type}`
}
return data
}