@@ -25,6 +25,12 @@ Use class-based controllers to handle websocket events. Helps to organize your c
25
25
``` typescript
26
26
import ' reflect-metadata' ;
27
27
```
28
+
29
+ 3 . Install a DI container, for example ` typedi ` ;
30
+
31
+ ```
32
+ npm install typedi
33
+ ```
28
34
29
35
## Example of usage
30
36
@@ -39,8 +45,10 @@ Use class-based controllers to handle websocket events. Helps to organize your c
39
45
MessageBody ,
40
46
OnMessage ,
41
47
} from ' socket-controllers' ;
48
+ import {Service } from ' typedi' ; // Only if you are using typedi
42
49
43
50
@SocketController ()
51
+ @Service () // Only if you are using typedi
44
52
export class MessageController {
45
53
@OnConnect ()
46
54
connection(@ConnectedSocket () socket : any ) {
@@ -67,10 +75,13 @@ Use class-based controllers to handle websocket events. Helps to organize your c
67
75
``` typescript
68
76
import ' es6-shim' ; // this shim is optional if you are using old version of node
69
77
import ' reflect-metadata' ; // this shim is required
70
- import { createSocketServer } from ' socket-controllers' ;
71
- import { MessageController } from ' ./MessageController' ;
78
+ import { SocketControllers } from ' socket-controllers' ;
79
+ import { MessageController } from ' ./MessageController' ;
80
+ import {Container } from ' typedi' ; // Only if you are using typedi
72
81
73
- createSocketServer (3001 , {
82
+ new SocketControllers ({
83
+ port: 3001 ,
84
+ container: Container ,
74
85
controllers: [MessageController ],
75
86
});
76
87
```
@@ -135,7 +146,7 @@ export class MessageController {
135
146
136
147
If you specify a class type to parameter that is decorated with ` @MessageBody() ` ,
137
148
socket-controllers will use [ class-transformer] [ 1 ] to create instance of the given class type with the data received in the message.
138
- To disable this behaviour you need to specify a ` { useConstructorUtils: false } ` in SocketControllerOptions when creating a server.
149
+ To disable this behaviour you need to specify a ` { transformOption: { transform: false ] } ` in SocketControllerOptions when creating a server.
139
150
140
151
#### ` @SocketQueryParam() ` decorator
141
152
@@ -266,16 +277,18 @@ If promise returned by controller action, message will be emitted only after pro
266
277
#### Using exist server instead of creating a new one
267
278
268
279
If you need to create and configure socket.io server manually,
269
- you can use ` useSocketServer ` instead of ` createSocketServer ` function .
280
+ you can pass it to the ` SocketControllers ` constructor .
270
281
Here is example of creating socket.io server and configuring it with express:
271
282
272
283
``` typescript
273
284
import ' reflect-metadata' ; // this shim is required
274
- import { useSocketServer } from ' socket-controllers' ;
285
+ import { SocketControllers } from ' socket-controllers' ;
286
+ import { Server } from ' socket.io' ;
287
+ import { Container } from ' typedi' ; // Only if you are using typedi
275
288
276
289
const app = require (' express' )();
277
290
const server = require (' http' ).Server (app );
278
- const io = require ( ' socket.io ' ) (server );
291
+ const io = new Server (server );
279
292
280
293
server .listen (3001 );
281
294
@@ -287,7 +300,7 @@ io.use((socket: any, next: Function) => {
287
300
console .log (' Custom middleware' );
288
301
next ();
289
302
});
290
- useSocketServer ( io );
303
+ new SocketControllers ({ io , container: Container } );
291
304
```
292
305
293
306
#### Load all controllers from the given directory
@@ -297,9 +310,12 @@ You can load all controllers in once from specific directories, by specifying ar
297
310
298
311
``` typescript
299
312
import ' reflect-metadata' ; // this shim is required
300
- import { createSocketServer } from ' socket-controllers' ;
313
+ import { SocketControllers } from ' socket-controllers' ;
314
+ import { Container } from ' typedi' ; // Only if you are using typedi
301
315
302
- createSocketServer (3000 , {
316
+ new SocketControllers ({
317
+ port: 3000 ,
318
+ container: Container ,
303
319
controllers: [__dirname + ' /controllers/*.js' ],
304
320
}); // registers all given controllers
305
321
```
@@ -362,10 +378,13 @@ Controllers and middlewares should be loaded:
362
378
363
379
``` typescript
364
380
import ' reflect-metadata' ;
365
- import { createSocketServer } from ' socket-controllers' ;
381
+ import { SocketControllers } from ' socket-controllers' ;
366
382
import { MessageController } from ' ./MessageController' ;
367
383
import { MyMiddleware } from ' ./MyMiddleware' ; // here we import it
368
- let io = createSocketServer (3000 , {
384
+ import { Container } from ' typedi' ; // Only if you are using typedi
385
+ const server = new SocketControllers ({
386
+ port: 3000 ,
387
+ container: Container ,
369
388
controllers: [MessageController ],
370
389
middlewares: [MyMiddleware ],
371
390
});
@@ -375,10 +394,13 @@ Also you can load them from directories. Also you can use glob patterns:
375
394
376
395
``` typescript
377
396
import ' reflect-metadata' ;
378
- import { createSocketServer } from ' socket-controllers' ;
379
- let io = createSocketServer (3000 , {
380
- controllers: [__dirname + ' /controllers/**/*.js' ],
381
- middlewares: [__dirname + ' /middlewares/**/*.js' ],
397
+ import { SocketControllers } from ' socket-controllers' ;
398
+ import { Container } from ' typedi' ; // Only if you are using typedi
399
+ const server = new SocketControllers ({
400
+ port: 3000 ,
401
+ container: Container ,
402
+ controllers: [__dirname + ' /controllers/**/*.js' ],
403
+ middlewares: [__dirname + ' /middlewares/**/*.js' ],
382
404
});
383
405
```
384
406
@@ -390,15 +412,13 @@ Here is example how to integrate socket-controllers with [typedi](https://github
390
412
391
413
``` typescript
392
414
import ' reflect-metadata' ;
393
- import { createSocketServer , useContainer } from ' socket-controllers' ;
415
+ import { SocketControllers } from ' socket-controllers' ;
394
416
import { Container } from ' typedi' ;
395
417
396
- // its important to set container before any operation you do with socket-controllers,
397
- // including importing controllers
398
- useContainer (Container );
399
-
400
418
// create and run socket server
401
- let io = createSocketServer (3000 , {
419
+ const server = new SocketControllers ({
420
+ port: 3000 ,
421
+ container: Container ,
402
422
controllers: [__dirname + ' /controllers/*.js' ],
403
423
middlewares: [__dirname + ' /middlewares/*.js' ],
404
424
});
0 commit comments