feat(wetsock) auto parse incoming messages as JSON

This commit is contained in:
Benjamin Wegener
2024-04-02 23:39:16 +00:00
parent ed6a254bbb
commit 22b74ba0b9

View File

@@ -32,6 +32,14 @@ export default class WetSock {
handleMessage(ws, message) {
const client = this.#clients.get(ws.data.clientId);
try {
const messageData = JSON.parse(message);
if(typeof(messageData) == 'object' && messageData.constructor.name == 'Object') {
message = messageData;
}
} catch (err) {}
for(const plugin of this.#plugins) {
if(plugin.onMessage(ws.data.clientId, message)) break; // plugin consumes message by returning a truthy value
}