1
1
import { INestApplicationContext , Logger } from '@nestjs/common' ;
2
2
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' ;
4
4
import { AbstractWsAdapter } from '@nestjs/websockets' ;
5
5
import {
6
6
CLOSE_EVENT ,
@@ -49,9 +49,13 @@ export class WsAdapter extends AbstractWsAdapter {
49
49
50
50
public create (
51
51
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
+ } ,
53
57
) {
54
- const { server, ...wsOptions } = options ;
58
+ const { server, path , ...wsOptions } = options ;
55
59
if ( wsOptions ?. namespace ) {
56
60
const error = new Error (
57
61
'"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 {
69
73
} ) ,
70
74
) ;
71
75
72
- this . addWsServerToRegistry ( wsServer , port , options . path || '/' ) ;
76
+ this . addWsServerToRegistry ( wsServer , port , path ) ;
73
77
return wsServer ;
74
78
}
75
79
76
80
if ( server ) {
77
81
return server ;
78
82
}
79
- if ( options . path && port !== UNDERLYING_HTTP_SERVER_PORT ) {
83
+ if ( path && port !== UNDERLYING_HTTP_SERVER_PORT ) {
80
84
// Multiple servers with different paths
81
85
// sharing a single HTTP/S server running on different port
82
86
// than a regular HTTP application
@@ -89,12 +93,13 @@ export class WsAdapter extends AbstractWsAdapter {
89
93
...wsOptions ,
90
94
} ) ,
91
95
) ;
92
- this . addWsServerToRegistry ( wsServer , port , options . path ) ;
96
+ this . addWsServerToRegistry ( wsServer , port , path ) ;
93
97
return wsServer ;
94
98
}
95
99
const wsServer = this . bindErrorHandler (
96
100
new wsPackage . Server ( {
97
101
port,
102
+ path,
98
103
...wsOptions ,
99
104
} ) ,
100
105
) ;
@@ -202,7 +207,7 @@ export class WsAdapter extends AbstractWsAdapter {
202
207
const entries = this . wsServersRegistry . get ( port ) ?? [ ] ;
203
208
entries . push ( wsServer ) ;
204
209
205
- wsServer . path = path ;
210
+ wsServer . path = normalizePath ( path ) ;
206
211
this . wsServersRegistry . set ( port , entries ) ;
207
212
}
208
213
}
0 commit comments