Fix websocket port issue

* Make 'ws' (collaboration) and socket.io work on same server port
This commit is contained in:
Philipinho
2023-10-20 22:56:59 +01:00
parent 9b682c8af5
commit c6477698c0
3 changed files with 51 additions and 15 deletions

View File

@ -38,7 +38,6 @@
"@nestjs/mapped-types": "^2.0.2",
"@nestjs/platform-fastify": "^10.2.7",
"@nestjs/platform-socket.io": "^10.2.7",
"@nestjs/platform-ws": "^10.2.7",
"@nestjs/typeorm": "^10.0.0",
"@nestjs/websockets": "^10.2.7",
"bcrypt": "^5.1.1",

View File

@ -0,0 +1,39 @@
import { WebSocketServer } from 'ws';
export class CollabWsAdapter {
private readonly wss: WebSocketServer;
constructor() {
this.wss = new WebSocketServer({ noServer: true });
}
handleUpgrade(path: string, httpServer) {
httpServer.on('upgrade', (request, socket, head) => {
try {
const baseUrl = 'ws://' + request.headers.host + '/';
const pathname = new URL(request.url, baseUrl).pathname;
if (pathname === path) {
this.wss.handleUpgrade(request, socket, head, (ws) => {
this.wss.emit('connection', ws, request);
});
} else if (pathname === '/socket.io/') {
return;
} else {
socket.destroy();
}
} catch (err) {
socket.end('HTTP/1.1 400\r\n' + err.message);
}
});
return this.wss;
}
public close() {
this.wss.clients.forEach((client) => {
client.terminate();
});
this.wss.close();
}
}

View File

@ -6,9 +6,9 @@ import { PersistenceExtension } from './extensions/persistence.extension';
import { PageModule } from '../core/page/page.module';
import { CollaborationGateway } from './collaboration.gateway';
import { HttpAdapterHost } from '@nestjs/core';
import { WsAdapter } from '@nestjs/platform-ws';
import WebSocket from 'ws';
import { CollabWsAdapter } from './adapter/collab-ws.adapter';
import { IncomingMessage } from 'http';
import { WebSocket } from 'ws';
@Module({
providers: [
@ -19,29 +19,27 @@ import { IncomingMessage } from 'http';
imports: [UserModule, AuthModule, PageModule],
})
export class CollaborationModule implements OnModuleInit, OnModuleDestroy {
private collabWsAdapter: CollabWsAdapter;
private path = '/collaboration';
constructor(
private readonly collaborationGateway: CollaborationGateway,
private readonly httpAdapterHost: HttpAdapterHost,
) {}
onModuleInit() {
const port = 0; // zero to reuse existing server port
const path = '/collaboration';
this.collabWsAdapter = new CollabWsAdapter();
const httpServer = this.httpAdapterHost.httpAdapter.getHttpServer();
const wsAdapter = new WsAdapter(httpServer).create(port, {
path,
});
wsAdapter.on(
'connection',
(client: WebSocket, request: IncomingMessage) => {
this.collaborationGateway.handleConnection(client, request);
},
);
const wss = this.collabWsAdapter.handleUpgrade(this.path, httpServer);
wss.on('connection', (client: WebSocket, request: IncomingMessage) => {
this.collaborationGateway.handleConnection(client, request);
});
}
onModuleDestroy(): any {
this.collaborationGateway.handleDestroy();
this.collabWsAdapter.close();
}
}