Skip to content

Commit c885a89

Browse files
Merge pull request #11188 from CodyTseng/fix-ws-multi-servers-on-different-paths
fix(ws): mount multi `ws` servers on different paths
2 parents 718e704 + e46147e commit c885a89

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

packages/platform-ws/adapters/ws-adapter.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { INestApplicationContext, Logger } from '@nestjs/common';
22
import { loadPackage } from '@nestjs/common/utils/load-package.util';
3-
import { isNil } from '@nestjs/common/utils/shared.utils';
3+
import { normalizePath, isNil } from '@nestjs/common/utils/shared.utils';
44
import { AbstractWsAdapter } from '@nestjs/websockets';
55
import {
66
CLOSE_EVENT,
@@ -49,9 +49,13 @@ export class WsAdapter extends AbstractWsAdapter {
4949

5050
public create(
5151
port: number,
52-
options?: Record<string, any> & { namespace?: string; server?: any },
52+
options?: Record<string, any> & {
53+
namespace?: string;
54+
server?: any;
55+
path?: string;
56+
},
5357
) {
54-
const { server, ...wsOptions } = options;
58+
const { server, path, ...wsOptions } = options;
5559
if (wsOptions?.namespace) {
5660
const error = new Error(
5761
'"WsAdapter" does not support namespaces. If you need namespaces in your project, consider using the "@nestjs/platform-socket.io" package instead.',
@@ -69,14 +73,14 @@ export class WsAdapter extends AbstractWsAdapter {
6973
}),
7074
);
7175

72-
this.addWsServerToRegistry(wsServer, port, options.path || '/');
76+
this.addWsServerToRegistry(wsServer, port, path);
7377
return wsServer;
7478
}
7579

7680
if (server) {
7781
return server;
7882
}
79-
if (options.path && port !== UNDERLYING_HTTP_SERVER_PORT) {
83+
if (path && port !== UNDERLYING_HTTP_SERVER_PORT) {
8084
// Multiple servers with different paths
8185
// sharing a single HTTP/S server running on different port
8286
// than a regular HTTP application
@@ -89,12 +93,13 @@ export class WsAdapter extends AbstractWsAdapter {
8993
...wsOptions,
9094
}),
9195
);
92-
this.addWsServerToRegistry(wsServer, port, options.path);
96+
this.addWsServerToRegistry(wsServer, port, path);
9397
return wsServer;
9498
}
9599
const wsServer = this.bindErrorHandler(
96100
new wsPackage.Server({
97101
port,
102+
path,
98103
...wsOptions,
99104
}),
100105
);
@@ -202,7 +207,7 @@ export class WsAdapter extends AbstractWsAdapter {
202207
const entries = this.wsServersRegistry.get(port) ?? [];
203208
entries.push(wsServer);
204209

205-
wsServer.path = path;
210+
wsServer.path = normalizePath(path);
206211
this.wsServersRegistry.set(port, entries);
207212
}
208213
}

0 commit comments

Comments
 (0)