mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-06-24 02:13:28 +00:00
Start WS routes implementation
This commit is contained in:
@ -11,6 +11,8 @@ import { randomStr } from '../utils/CryptUtils';
|
||||
import { EventsHelper } from '../helpers/EventsHelper';
|
||||
import { NotificationsHelper } from '../helpers/NotificationsHelper';
|
||||
import { ConversationsHelper } from '../helpers/ConversationsHelper';
|
||||
import { UserWebSocketRoutes } from './UserWebSocketRoutes';
|
||||
import { UserWebSocketRequestsHandler } from '../entities/WebSocketRequestHandler';
|
||||
import { WsMessage } from '../entities/WsMessage';
|
||||
|
||||
interface PendingRequests {
|
||||
@ -132,7 +134,7 @@ export class UserWebSocketController {
|
||||
})
|
||||
|
||||
// Handles incoming messages
|
||||
ws.addEventListener("message", (msg) => {
|
||||
ws.addEventListener("message", async (msg) => {
|
||||
|
||||
// Only accept text messages
|
||||
if(msg.type != "message") {
|
||||
@ -140,8 +142,45 @@ export class UserWebSocketController {
|
||||
ws.close();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Check if the data are valid
|
||||
let wsMsg : WsMessage;
|
||||
try {
|
||||
wsMsg = new WsMessage(JSON.parse(msg.data));
|
||||
|
||||
if(!wsMsg.isValidRequest)
|
||||
throw new Error("Requested message is invalid!");
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
ws.close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Create request handler
|
||||
const handler = new UserWebSocketRequestsHandler(client, wsMsg);
|
||||
|
||||
try {
|
||||
// Check if we support this kind of message
|
||||
const route = UserWebSocketRoutes.find((el) => el.title == wsMsg.title);
|
||||
|
||||
if(route == undefined) {
|
||||
handler.error(404, "Method not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
else
|
||||
await route.handler(handler);
|
||||
|
||||
} catch(e) {
|
||||
|
||||
// Try again to send again a response
|
||||
try {
|
||||
handler.sendResponse("error", "Server error.");
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user