Cleaned up HttpServer. WebSocket connects
This commit is contained in:
@@ -155,6 +155,11 @@ export default class HttpServer {
|
||||
this._eventEmitter.on(event, handler);
|
||||
}
|
||||
|
||||
|
||||
public getServer(): Nullable<Server> {
|
||||
return this._server.value;
|
||||
}
|
||||
|
||||
private configureListener() {
|
||||
if (!this._app.value) {
|
||||
throw new Error('Unable to configure listener, no app instance found');
|
||||
@@ -341,16 +346,19 @@ export default class HttpServer {
|
||||
}
|
||||
|
||||
const app = this._app.value;
|
||||
|
||||
|
||||
let catchAllHandler: Nullable<RequestHandler> = null;
|
||||
|
||||
const registerRoutes = (routes: Record<string, RequestHandler>): void => {
|
||||
|
||||
const registerRoutes = (method: string, routes: Record<string, RequestHandler>): void => {
|
||||
for (const route of Object.entries(routes)) {
|
||||
|
||||
if (!route) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const [name, handler] = route;
|
||||
|
||||
console.log(`[${name}] registering [%o]`, handler);
|
||||
if (name === '*') {
|
||||
console.log('Catch-all handler', name, handler);
|
||||
if (catchAllHandler) {
|
||||
throw new Error(`Only one catch-all handler can ber registered per server, ignoring conflicting catch-all`);
|
||||
}
|
||||
@@ -359,21 +367,15 @@ export default class HttpServer {
|
||||
continue;
|
||||
}
|
||||
|
||||
this._logger.debug(`Registering [GET] route [${name}] with [%o]`, handler);
|
||||
app.get(name, handler);
|
||||
this._logger.debug(`Registering [${method}] route [${name}] handler`);
|
||||
app[method.toLowerCase() as 'get' | 'post' | 'put' | 'patch' | 'delete'](name, handler);
|
||||
}
|
||||
}
|
||||
|
||||
const getRoutes = this._routes.getGETRoutes();
|
||||
const postRoutes = this._routes.getPOSTRoutes();
|
||||
const putRoutes = this._routes.getPUTRoutes();
|
||||
const patchRoutes = this._routes.getPATCHRoutes();
|
||||
const deleteRoutes = this._routes.getDELETERoutes();
|
||||
|
||||
registerRoutes(getRoutes);
|
||||
registerRoutes(postRoutes);
|
||||
registerRoutes(putRoutes);
|
||||
registerRoutes(patchRoutes);
|
||||
registerRoutes(deleteRoutes);
|
||||
registerRoutes('GET', this._routes.getGETRoutes());
|
||||
registerRoutes('POST', this._routes.getPOSTRoutes());
|
||||
registerRoutes('PUT', this._routes.getPUTRoutes());
|
||||
registerRoutes('PATCH', this._routes.getPATCHRoutes());
|
||||
registerRoutes('DELETE', this._routes.getDELETERoutes());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import ws, {WebSocketServer as WSServer, WebSocket} from 'ws';
|
||||
import Assert from '../../lang/Assert';
|
||||
import Strings from '../../lang/Strings';
|
||||
import WebsocketExtensions from 'websocket-extensions';
|
||||
import deflate from 'permessage-deflate';
|
||||
|
||||
interface ExtendedWebSocket extends WebSocket {
|
||||
id: string;
|
||||
@@ -100,11 +99,12 @@ export default class WebSocketServer {
|
||||
this._server = new WSServer(serverOptions);
|
||||
this._extensions = new WebsocketExtensions();
|
||||
|
||||
this._extensions.add(deflate());
|
||||
// this._extensions.add(deflate());
|
||||
(this._server as WSServer & {_server?: HttpServer})._server = this._httpServer;
|
||||
this._httpServer.on('error', this._server.emit.bind(this._server, 'error'));
|
||||
this._httpServer.on('listening', this._server.emit.bind(this._server, 'listening'));
|
||||
this._httpServer.on('upgrade', (req, socket, head) => {
|
||||
this._logger.debug(`[HttpServer] Upgrade to WebSocket: ${req.method} ${req.url}`);
|
||||
if (!req.url?.startsWith(path)) {
|
||||
this._logger.debug(`Skipping upgrade of http request due to incorrect path [${req.url}]`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user