From 348084442b93940feabfb8c46c11d0dbf0f41945 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 31 Mar 2020 14:53:46 +0200 Subject: [PATCH] Turn WsMessage into an object --- src/controllers/UserWebSocketController.ts | 15 +++++--------- src/entities/WebSocketRequestHandler.ts | 5 +++-- src/entities/WsMessage.ts | 24 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 src/entities/WsMessage.ts diff --git a/src/controllers/UserWebSocketController.ts b/src/controllers/UserWebSocketController.ts index 10078d3..084cc55 100644 --- a/src/controllers/UserWebSocketController.ts +++ b/src/controllers/UserWebSocketController.ts @@ -11,6 +11,7 @@ import { randomStr } from '../utils/CryptUtils'; import { EventsHelper } from '../helpers/EventsHelper'; import { NotificationsHelper } from '../helpers/NotificationsHelper'; import { ConversationsHelper } from '../helpers/ConversationsHelper'; +import { WsMessage } from '../entities/WsMessage'; interface PendingRequests { time: number, @@ -26,12 +27,6 @@ export interface ActiveClient { ws: ws } -export interface WsMessage { - id: string, - title: string, - data: any -} - // Tokens are valid only 10 seconds after they are generated const TOKENS_DURATION = 10 const TOKEN_LENGTH = 20 @@ -201,11 +196,11 @@ export class UserWebSocketController { continue; // Notify user - this.Send(userID, "", { + this.Send(userID, "", new WsMessage({ title: "number_notifs", id: "", data: await NotificationsHelper.CountUnread(userID) - }); + })); } } @@ -222,11 +217,11 @@ export class UserWebSocketController { continue; // Notify user - this.Send(userID, "", { + this.Send(userID, "", new WsMessage({ title: "number_unread_conversations", id: "", data: await ConversationsHelper.CountUnreadForUser(userID) - }); + })); } } } diff --git a/src/entities/WebSocketRequestHandler.ts b/src/entities/WebSocketRequestHandler.ts index 5f7d792..b386b8e 100644 --- a/src/entities/WebSocketRequestHandler.ts +++ b/src/entities/WebSocketRequestHandler.ts @@ -5,7 +5,8 @@ */ import { BaseRequestsHandler } from "./BaseRequestsHandler"; -import { ActiveClient, WsMessage } from "../controllers/UserWebSocketController"; +import { ActiveClient } from "../controllers/UserWebSocketController"; +import { WsMessage } from "./WsMessage"; export class UserWebSocketRequestsHandler extends BaseRequestsHandler { @@ -17,7 +18,7 @@ export class UserWebSocketRequestsHandler extends BaseRequestsHandler { return this.wsClient.userID; } - + protected getPostParam(name: string) { throw new Error("Method not implemented."); } diff --git a/src/entities/WsMessage.ts b/src/entities/WsMessage.ts new file mode 100644 index 0000000..702782d --- /dev/null +++ b/src/entities/WsMessage.ts @@ -0,0 +1,24 @@ +/** + * WebSocket message + * + * @author Pierre Hubert + */ + +export interface WsMessageBuilder { + id: string, + title: string, + data: any +} + +export class WsMessage implements WsMessageBuilder { + id: string; + title: string; + data: any; + + public constructor(info: WsMessageBuilder) { + for (const key in info) { + if (info.hasOwnProperty(key)) + this[key] = info[key]; + } + } +} \ No newline at end of file