From d4b624e5191fefecaa464484920601a474e2d59d Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 31 Mar 2020 14:42:53 +0200 Subject: [PATCH] Start to implement WebSockets route system --- src/controllers/UserWebSocketController.ts | 4 +-- src/controllers/UserWebSocketRoutes.ts | 17 ++++++++++ src/entities/WebSocketRequestHandler.ts | 37 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/controllers/UserWebSocketRoutes.ts create mode 100644 src/entities/WebSocketRequestHandler.ts diff --git a/src/controllers/UserWebSocketController.ts b/src/controllers/UserWebSocketController.ts index c34322b..10078d3 100644 --- a/src/controllers/UserWebSocketController.ts +++ b/src/controllers/UserWebSocketController.ts @@ -19,14 +19,14 @@ interface PendingRequests { token: string } -interface ActiveClient { +export interface ActiveClient { socketID: string, clientID: number, userID: number, ws: ws } -interface WsMessage { +export interface WsMessage { id: string, title: string, data: any diff --git a/src/controllers/UserWebSocketRoutes.ts b/src/controllers/UserWebSocketRoutes.ts new file mode 100644 index 0000000..244e251 --- /dev/null +++ b/src/controllers/UserWebSocketRoutes.ts @@ -0,0 +1,17 @@ +/** + * User Websocket requests route + * + * Note : this implementation requires the + * user to be signed in to perform requests + * + * @author Pierre Hubert + */ + +import { UserWebSocketRequestsHandler } from "../entities/WebSocketRequestHandler"; + +export interface UserWebSocketRoute { + title: string, + handler: (h: UserWebSocketRequestsHandler) => Promise +} + +export const UserWebSocketRoutes: UserWebSocketRoute[] = [] \ No newline at end of file diff --git a/src/entities/WebSocketRequestHandler.ts b/src/entities/WebSocketRequestHandler.ts new file mode 100644 index 0000000..5f7d792 --- /dev/null +++ b/src/entities/WebSocketRequestHandler.ts @@ -0,0 +1,37 @@ +/** + * User Web Sockets requests handler implementation + * + * @author Pierre Hubert + */ + +import { BaseRequestsHandler } from "./BaseRequestsHandler"; +import { ActiveClient, WsMessage } from "../controllers/UserWebSocketController"; + +export class UserWebSocketRequestsHandler extends BaseRequestsHandler { + + constructor(private wsClient: ActiveClient, private req: WsMessage) { + super(); + } + + protected get userID(): number { + return this.wsClient.userID; + } + + + 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."); + } + +} \ No newline at end of file