From 587f482736a419ea5cf0ca8b41161dd3b3916e7c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 30 Mar 2020 14:45:29 +0200 Subject: [PATCH] Accept only text messages on WebSocket --- src/controllers/UserWebSocketController.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/controllers/UserWebSocketController.ts b/src/controllers/UserWebSocketController.ts index c446176..c7854aa 100644 --- a/src/controllers/UserWebSocketController.ts +++ b/src/controllers/UserWebSocketController.ts @@ -118,8 +118,26 @@ export class UserWebSocketController { this.active_clients.splice(this.active_clients.indexOf(client), 1); }) + // Handles error + ws.addEventListener("error", (e) => { + if(ws.readyState == ws.OPEN) + ws.close(); + + console.log("WebSocket error", e) + }) - console.log(this.active_clients) + // Handles incoming messages + ws.addEventListener("message", (msg) => { + + // Only accept text messages + if(msg.type != "message") { + console.error("Received a non-text messsage through a WebSocket !") + ws.close(); + return; + } + + + }) } /**