mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 13:29:22 +00:00
Listen for websockets
This commit is contained in:
parent
a58452af3e
commit
70a5c910d2
@ -16,6 +16,9 @@ import { CommentsController } from "./CommentsController";
|
|||||||
import { LikesController } from "./LikesController";
|
import { LikesController } from "./LikesController";
|
||||||
import { SurveyController } from "./SurveyController";
|
import { SurveyController } from "./SurveyController";
|
||||||
import { SettingsController } from "./SettingsController";
|
import { SettingsController } from "./SettingsController";
|
||||||
|
import { Request } from "express";
|
||||||
|
import * as ws from 'ws';
|
||||||
|
import { UserWS } from "./UserWebSocketController";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controllers routes
|
* Controllers routes
|
||||||
@ -25,7 +28,8 @@ import { SettingsController } from "./SettingsController";
|
|||||||
|
|
||||||
export enum RouteType {
|
export enum RouteType {
|
||||||
POST, // Default
|
POST, // Default
|
||||||
GET
|
GET,
|
||||||
|
WS, // Special: WebSockets
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Route {
|
export interface Route {
|
||||||
@ -33,10 +37,16 @@ export interface Route {
|
|||||||
path: string,
|
path: string,
|
||||||
cb: (req : RequestHandler) => Promise<void> | void,
|
cb: (req : RequestHandler) => Promise<void> | void,
|
||||||
needLogin ?: boolean, // Default = true
|
needLogin ?: boolean, // Default = true
|
||||||
|
|
||||||
|
// Specific for websockets
|
||||||
|
wsCallback ?: (req: Request, ws: ws) => Promise<void> | void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Routes : Route[] = [
|
export const Routes : Route[] = [
|
||||||
|
|
||||||
|
// Main user websocket
|
||||||
|
{type: RouteType.WS, path: "/ws", cb: () => {throw Error()}, wsCallback: UserWS },
|
||||||
|
|
||||||
// Welcome controller
|
// Welcome controller
|
||||||
{type: RouteType.GET, path: "/", cb: WelcomeController.HomeMessage, needLogin: false},
|
{type: RouteType.GET, path: "/", cb: WelcomeController.HomeMessage, needLogin: false},
|
||||||
|
|
||||||
|
11
src/controllers/UserWebSocketController.ts
Normal file
11
src/controllers/UserWebSocketController.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* User websocket controller
|
||||||
|
*
|
||||||
|
* @author Pierre Hubert
|
||||||
|
*/
|
||||||
|
import * as ws from 'ws';
|
||||||
|
import { Request } from 'express';
|
||||||
|
|
||||||
|
export async function UserWS(req: Request, ws: ws) {
|
||||||
|
|
||||||
|
}
|
30
src/main.ts
30
src/main.ts
@ -4,6 +4,8 @@ import { DatabaseHelper } from "./helpers/DatabaseHelper";
|
|||||||
import { Routes, RouteType } from './controllers/Routes';
|
import { Routes, RouteType } from './controllers/Routes';
|
||||||
import { RequestHandler } from './entities/RequestHandler';
|
import { RequestHandler } from './entities/RequestHandler';
|
||||||
import * as fileUpload from 'express-fileupload';
|
import * as fileUpload from 'express-fileupload';
|
||||||
|
import * as expressWs from 'express-ws';
|
||||||
|
import * as ws from 'ws';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main project script
|
* Main project script
|
||||||
@ -22,7 +24,7 @@ async function init() {
|
|||||||
await DatabaseHelper.connect();
|
await DatabaseHelper.connect();
|
||||||
|
|
||||||
// Start HTTP Server
|
// Start HTTP Server
|
||||||
const app = express();
|
const app = expressWs(express()).app;
|
||||||
|
|
||||||
app.use(express.urlencoded({extended: true}));
|
app.use(express.urlencoded({extended: true}));
|
||||||
|
|
||||||
@ -53,11 +55,29 @@ async function init() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if(route.type == RouteType.GET)
|
// WebSocket callback
|
||||||
app.get(route.path, cb);
|
const wsCB = async (ws: ws, req: express.Request) => {
|
||||||
|
try {
|
||||||
|
await route.wsCallback(req, ws);
|
||||||
|
} catch(e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
else
|
// Call the appropriate function
|
||||||
app.post(route.path, cb);
|
switch(route.type) {
|
||||||
|
case RouteType.GET:
|
||||||
|
app.get(route.path, cb);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case RouteType.WS:
|
||||||
|
app.ws(route.path, wsCB);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
app.post(route.path, cb);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user