@@ -6,6 +6,7 @@ import {ParamMetadata} from "./metadata/ParamMetadata";
6
6
import { ParameterParseJsonError } from "./error/ParameterParseJsonError" ;
7
7
import { ParamTypes } from "./metadata/types/ParamTypes" ;
8
8
import { ControllerMetadata } from "./metadata/ControllerMetadata" ;
9
+ import * as pathToRegexp from "path-to-regexp" ;
9
10
10
11
/**
11
12
* Registers controllers and actions in the given server framework.
@@ -91,7 +92,11 @@ export class SocketControllerExecutor {
91
92
92
93
// register controllers with namespaces
93
94
controllersWithNamespaces . forEach ( controller => {
94
- this . io . of ( controller . namespace ) . on ( "connection" , ( socket : any ) => this . handleConnection ( [ controller ] , socket ) ) ;
95
+ let namespace : string | RegExp = controller . namespace ;
96
+ if ( ! ( namespace instanceof RegExp ) ) {
97
+ namespace = pathToRegexp ( namespace ) ;
98
+ }
99
+ this . io . of ( namespace ) . on ( "connection" , ( socket : any ) => this . handleConnection ( [ controller ] , socket ) ) ;
95
100
} ) ;
96
101
97
102
return this ;
@@ -101,20 +106,20 @@ export class SocketControllerExecutor {
101
106
controllers . forEach ( controller => {
102
107
controller . actions . forEach ( action => {
103
108
if ( action . type === ActionTypes . CONNECT ) {
104
- this . handleAction ( action , { socket : socket } )
109
+ this . handleAction ( action , { socket : socket } )
105
110
. then ( result => this . handleSuccessResult ( result , action , socket ) )
106
111
. catch ( error => this . handleFailResult ( error , action , socket ) ) ;
107
112
108
113
} else if ( action . type === ActionTypes . DISCONNECT ) {
109
114
socket . on ( "disconnect" , ( ) => {
110
- this . handleAction ( action , { socket : socket } )
115
+ this . handleAction ( action , { socket : socket } )
111
116
. then ( result => this . handleSuccessResult ( result , action , socket ) )
112
117
. catch ( error => this . handleFailResult ( error , action , socket ) ) ;
113
118
} ) ;
114
119
115
120
} else if ( action . type === ActionTypes . MESSAGE ) {
116
121
socket . on ( action . name , ( data : any ) => {
117
- this . handleAction ( action , { socket : socket , data : data } )
122
+ this . handleAction ( action , { socket : socket , data : data } )
118
123
. then ( result => this . handleSuccessResult ( result , action , socket ) )
119
124
. catch ( error => this . handleFailResult ( error , action , socket ) ) ;
120
125
} ) ;
@@ -123,8 +128,8 @@ export class SocketControllerExecutor {
123
128
} ) ;
124
129
}
125
130
126
- private handleAction ( action : ActionMetadata , options : { socket ?: any , data ?: any } ) : Promise < any > {
127
-
131
+ private handleAction ( action : ActionMetadata , options : { socket ?: any , data ?: any } ) : Promise < any > {
132
+
128
133
// compute all parameters
129
134
const paramsPromises = action . params
130
135
. sort ( ( param1 , param2 ) => param1 . index - param2 . index )
@@ -147,6 +152,13 @@ export class SocketControllerExecutor {
147
152
} else if ( param . type === ParamTypes . SOCKET_ROOMS ) {
148
153
return options . socket . rooms ;
149
154
155
+ } else if ( param . type === ParamTypes . NAMESPACE_PARAMS ) {
156
+ return this . handleNamespaceParams ( options . socket , action , param ) ;
157
+
158
+ } else if ( param . type === ParamTypes . NAMESPACE_PARAM ) {
159
+ const params : any [ ] = this . handleNamespaceParams ( options . socket , action , param ) ;
160
+ return params [ param . value ] ;
161
+
150
162
} else {
151
163
return this . handleParam ( param , options ) ;
152
164
}
@@ -162,7 +174,7 @@ export class SocketControllerExecutor {
162
174
} ) ;
163
175
}
164
176
165
- private handleParam ( param : ParamMetadata , options : { socket ?: any , data ?: any } ) {
177
+ private handleParam ( param : ParamMetadata , options : { socket ?: any , data ?: any } ) {
166
178
167
179
let value = options . data ;
168
180
if ( value !== null && value !== undefined && value !== "" )
@@ -241,4 +253,15 @@ export class SocketControllerExecutor {
241
253
}
242
254
}
243
255
256
+ private handleNamespaceParams ( socket : any , action : ActionMetadata , param : ParamMetadata ) : any [ ] {
257
+ const keys : any [ ] = [ ] ;
258
+ const regexp = pathToRegexp ( action . controllerMetadata . namespace , keys ) ;
259
+ const parts : any [ ] = regexp . exec ( socket . nsp . name ) ;
260
+ const params : any [ ] = [ ] ;
261
+ keys . forEach ( ( key : any , index : number ) => {
262
+ params [ key . name ] = this . handleParamFormat ( parts [ index + 1 ] , param ) ;
263
+ } ) ;
264
+ return params ;
265
+ }
266
+
244
267
}
0 commit comments