1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-03-14 01:52:38 +00:00
comunicapiv2/src/entities/WebSocketRequestHandler.ts

58 lines
1.4 KiB
TypeScript
Raw Normal View History

/**
* User Web Sockets requests handler implementation
*
* @author Pierre Hubert
*/
import { BaseRequestsHandler } from "./BaseRequestsHandler";
2020-03-31 16:20:35 +02:00
import { ActiveClient, UserWebSocketController } from "../controllers/UserWebSocketController";
2020-03-31 14:53:46 +02:00
import { WsMessage } from "./WsMessage";
export class UserWebSocketRequestsHandler extends BaseRequestsHandler {
2020-03-31 16:20:35 +02:00
private sentResponse = false;
constructor(private wsClient: ActiveClient, private req: WsMessage) {
super();
}
protected get userID(): number {
return this.wsClient.userID;
}
2020-03-31 14:53:46 +02:00
protected getPostParam(name: string) {
throw new Error("Method not implemented.");
}
2020-03-31 16:20:35 +02:00
public hasPostParameter(name: string): boolean {
throw new Error("Method not implemented.");
}
2020-03-31 16:20:35 +02:00
public error(code: number, message: string): void {
2020-03-31 16:20:35 +02:00
this.sendResponse("error", code + " - " + message);
throw new Error("An error occured while processing user WS request!");
}
2020-03-31 16:20:35 +02:00
public success(message: string): void {
2020-03-31 16:20:35 +02:00
this.sendResponse("success", message);
}
2020-03-31 16:20:35 +02:00
public send(data: any): void {
2020-03-31 16:20:35 +02:00
this.sendResponse("success", data);
}
2020-03-31 16:20:35 +02:00
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;
}
}