mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-06-20 00:25:17 +00:00
Start WS routes implementation
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
@ -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";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user