2020-03-31 14:42:53 +02:00
|
|
|
/**
|
|
|
|
* 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";
|
2020-03-31 14:42:53 +02:00
|
|
|
|
|
|
|
export class UserWebSocketRequestsHandler extends BaseRequestsHandler {
|
|
|
|
|
2020-03-31 16:20:35 +02:00
|
|
|
private sentResponse = false;
|
|
|
|
|
2020-03-31 14:42:53 +02:00
|
|
|
constructor(private wsClient: ActiveClient, private req: WsMessage) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected get userID(): number {
|
|
|
|
return this.wsClient.userID;
|
|
|
|
}
|
|
|
|
|
2020-03-31 14:53:46 +02:00
|
|
|
|
2020-03-31 14:42:53 +02:00
|
|
|
protected getPostParam(name: string) {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
2020-03-31 16:20:35 +02:00
|
|
|
|
2020-03-31 14:42:53 +02:00
|
|
|
public hasPostParameter(name: string): boolean {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
2020-03-31 16:20:35 +02:00
|
|
|
|
2020-03-31 14:42:53 +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 14:42:53 +02:00
|
|
|
}
|
2020-03-31 16:20:35 +02:00
|
|
|
|
2020-03-31 14:42:53 +02:00
|
|
|
public success(message: string): void {
|
2020-03-31 16:20:35 +02:00
|
|
|
this.sendResponse("success", message);
|
2020-03-31 14:42:53 +02:00
|
|
|
}
|
2020-03-31 16:20:35 +02:00
|
|
|
|
2020-03-31 14:42:53 +02:00
|
|
|
public send(data: any): void {
|
2020-03-31 16:20:35 +02:00
|
|
|
this.sendResponse("success", data);
|
2020-03-31 14:42:53 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2020-03-31 14:42:53 +02:00
|
|
|
}
|