mirror of
https://github.com/docmost/docmost.git
synced 2025-11-14 03:31:11 +10:00
Fix websocket port issue
* Make 'ws' (collaboration) and socket.io work on same server port
This commit is contained in:
@ -38,7 +38,6 @@
|
|||||||
"@nestjs/mapped-types": "^2.0.2",
|
"@nestjs/mapped-types": "^2.0.2",
|
||||||
"@nestjs/platform-fastify": "^10.2.7",
|
"@nestjs/platform-fastify": "^10.2.7",
|
||||||
"@nestjs/platform-socket.io": "^10.2.7",
|
"@nestjs/platform-socket.io": "^10.2.7",
|
||||||
"@nestjs/platform-ws": "^10.2.7",
|
|
||||||
"@nestjs/typeorm": "^10.0.0",
|
"@nestjs/typeorm": "^10.0.0",
|
||||||
"@nestjs/websockets": "^10.2.7",
|
"@nestjs/websockets": "^10.2.7",
|
||||||
"bcrypt": "^5.1.1",
|
"bcrypt": "^5.1.1",
|
||||||
|
|||||||
39
server/src/collaboration/adapter/collab-ws.adapter.ts
Normal file
39
server/src/collaboration/adapter/collab-ws.adapter.ts
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,9 +6,9 @@ import { PersistenceExtension } from './extensions/persistence.extension';
|
|||||||
import { PageModule } from '../core/page/page.module';
|
import { PageModule } from '../core/page/page.module';
|
||||||
import { CollaborationGateway } from './collaboration.gateway';
|
import { CollaborationGateway } from './collaboration.gateway';
|
||||||
import { HttpAdapterHost } from '@nestjs/core';
|
import { HttpAdapterHost } from '@nestjs/core';
|
||||||
import { WsAdapter } from '@nestjs/platform-ws';
|
import { CollabWsAdapter } from './adapter/collab-ws.adapter';
|
||||||
import WebSocket from 'ws';
|
|
||||||
import { IncomingMessage } from 'http';
|
import { IncomingMessage } from 'http';
|
||||||
|
import { WebSocket } from 'ws';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
providers: [
|
providers: [
|
||||||
@ -19,29 +19,27 @@ import { IncomingMessage } from 'http';
|
|||||||
imports: [UserModule, AuthModule, PageModule],
|
imports: [UserModule, AuthModule, PageModule],
|
||||||
})
|
})
|
||||||
export class CollaborationModule implements OnModuleInit, OnModuleDestroy {
|
export class CollaborationModule implements OnModuleInit, OnModuleDestroy {
|
||||||
|
private collabWsAdapter: CollabWsAdapter;
|
||||||
|
private path = '/collaboration';
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly collaborationGateway: CollaborationGateway,
|
private readonly collaborationGateway: CollaborationGateway,
|
||||||
private readonly httpAdapterHost: HttpAdapterHost,
|
private readonly httpAdapterHost: HttpAdapterHost,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
onModuleInit() {
|
onModuleInit() {
|
||||||
const port = 0; // zero to reuse existing server port
|
this.collabWsAdapter = new CollabWsAdapter();
|
||||||
const path = '/collaboration';
|
|
||||||
|
|
||||||
const httpServer = this.httpAdapterHost.httpAdapter.getHttpServer();
|
const httpServer = this.httpAdapterHost.httpAdapter.getHttpServer();
|
||||||
const wsAdapter = new WsAdapter(httpServer).create(port, {
|
|
||||||
path,
|
|
||||||
});
|
|
||||||
|
|
||||||
wsAdapter.on(
|
const wss = this.collabWsAdapter.handleUpgrade(this.path, httpServer);
|
||||||
'connection',
|
|
||||||
(client: WebSocket, request: IncomingMessage) => {
|
wss.on('connection', (client: WebSocket, request: IncomingMessage) => {
|
||||||
this.collaborationGateway.handleConnection(client, request);
|
this.collaborationGateway.handleConnection(client, request);
|
||||||
},
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onModuleDestroy(): any {
|
onModuleDestroy(): any {
|
||||||
this.collaborationGateway.handleDestroy();
|
this.collaborationGateway.handleDestroy();
|
||||||
|
this.collabWsAdapter.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user