diff --git a/src/controllers/UserWebSocketController.ts b/src/controllers/UserWebSocketController.ts index 084cc55..43b0c32 100644 --- a/src/controllers/UserWebSocketController.ts +++ b/src/controllers/UserWebSocketController.ts @@ -11,6 +11,8 @@ import { randomStr } from '../utils/CryptUtils'; import { EventsHelper } from '../helpers/EventsHelper'; import { NotificationsHelper } from '../helpers/NotificationsHelper'; import { ConversationsHelper } from '../helpers/ConversationsHelper'; +import { UserWebSocketRoutes } from './UserWebSocketRoutes'; +import { UserWebSocketRequestsHandler } from '../entities/WebSocketRequestHandler'; import { WsMessage } from '../entities/WsMessage'; interface PendingRequests { @@ -132,7 +134,7 @@ export class UserWebSocketController { }) // Handles incoming messages - ws.addEventListener("message", (msg) => { + ws.addEventListener("message", async (msg) => { // Only accept text messages if(msg.type != "message") { @@ -140,8 +142,45 @@ export class UserWebSocketController { ws.close(); return; } - + // Check if the data are valid + let wsMsg : WsMessage; + try { + wsMsg = new WsMessage(JSON.parse(msg.data)); + + if(!wsMsg.isValidRequest) + throw new Error("Requested message is invalid!"); + } catch(e) { + console.error(e); + ws.close(); + return; + } + + // Create request handler + const handler = new UserWebSocketRequestsHandler(client, wsMsg); + + try { + // Check if we support this kind of message + const route = UserWebSocketRoutes.find((el) => el.title == wsMsg.title); + + if(route == undefined) { + handler.error(404, "Method not found!"); + return; + } + + else + await route.handler(handler); + + } catch(e) { + + // Try again to send again a response + try { + handler.sendResponse("error", "Server error."); + } catch(e) { + console.error(e); + } + + } }) } diff --git a/src/entities/WebSocketRequestHandler.ts b/src/entities/WebSocketRequestHandler.ts index b386b8e..43e4f0a 100644 --- a/src/entities/WebSocketRequestHandler.ts +++ b/src/entities/WebSocketRequestHandler.ts @@ -5,11 +5,13 @@ */ import { BaseRequestsHandler } from "./BaseRequestsHandler"; -import { ActiveClient } from "../controllers/UserWebSocketController"; +import { ActiveClient, UserWebSocketController } from "../controllers/UserWebSocketController"; import { WsMessage } from "./WsMessage"; export class UserWebSocketRequestsHandler extends BaseRequestsHandler { + private sentResponse = false; + constructor(private wsClient: ActiveClient, private req: WsMessage) { super(); } @@ -22,17 +24,35 @@ export class UserWebSocketRequestsHandler extends BaseRequestsHandler { protected getPostParam(name: string) { throw new Error("Method not implemented."); } + public hasPostParameter(name: string): boolean { throw new Error("Method not implemented."); } + public error(code: number, message: string): void { - throw new Error("Method not implemented."); - } - public success(message: string): void { - throw new Error("Method not implemented."); - } - public send(data: any): void { - throw new Error("Method not implemented."); + this.sendResponse("error", code + " - " + message); + throw new Error("An error occured while processing user WS request!"); } + public success(message: string): void { + this.sendResponse("success", message); + } + + public send(data: any): void { + this.sendResponse("success", data); + } + + public sendResponse(title: string, data: any) { + if(this.sentResponse) + throw new Error("Trying to send a response to a request to which a response has already been sent!") + + // Send the response + UserWebSocketController.Send(this.wsClient.userID, this.wsClient.socketID, new WsMessage({ + title: title, + data: data, + id: this.req.id + })) + + this.sentResponse = true; + } } \ No newline at end of file diff --git a/src/entities/WsMessage.ts b/src/entities/WsMessage.ts index 702782d..7534cb7 100644 --- a/src/entities/WsMessage.ts +++ b/src/entities/WsMessage.ts @@ -21,4 +21,12 @@ export class WsMessage implements WsMessageBuilder { this[key] = info[key]; } } + + get isValidRequest() : boolean { + return typeof this.id === "string" + && typeof this.title === "string" + && this.id.length > 0 + && this.title.length > 0 + && typeof this.data === "object"; + } } \ No newline at end of file