feat(@nestjs/websockets) ability to use custom socket.io adapter

This commit is contained in:
kamil.mysliwiec
2017-05-24 23:41:54 +02:00
parent becd8c3445
commit b39d84722c
11 changed files with 87 additions and 11 deletions

View File

@@ -1,3 +1,13 @@
## 2.1.1 (24.05.2017)
**@nestjs/common**, **@nestjs/microservices**
- `INestApplication` and `INestMicroservice` has new method now - `setIoAdapter()`,
- Ability to use custom `IoAdapter`
## 2.1.0 (22.05.2017)
**@nestjs/common**, **@nestjs/core**
- `INestApplication` has new methods now - `init()`, `setGlobalPrefix()`, `connectMicroservice()`, `close()`, `startAllMicroservices()`,
- `INestMicroservice` has new method - `close()`
## 2.0.3 (15.05.2017)
**@nestjs/common**
- `Req()` (`Request()`) and `Res()` (`Response()`) aliases to avoid conflicts with express typings

View File

@@ -1,3 +1,4 @@
import * as io from 'socket.io';
import { NestFactory } from './../src/core';
import { ApplicationModule } from './modules/app.module';
import { Transport } from '../src/microservices/index';
@@ -8,7 +9,14 @@ const microservice = app.connectMicroservice({
transport: Transport.TCP,
port: 5667,
});
app.setIoAdapter({
create(p: number) {
return io(p);
},
createWithNamespace(p: number, namespace: string) {
return io(p);
},
});
app.startAllMicroservices(() => console.log('All microservices are listening...'));
app.listen(port, () => {
console.log('Application listen on port:', port);

28
gulpfile.js Normal file
View File

@@ -0,0 +1,28 @@
const gulp = require('gulp');
const ts = require('gulp-typescript');
const packages = {
common: ts.createProject('src/common/tsconfig.json'),
core: ts.createProject('src/core/tsconfig.json'),
microservices: ts.createProject('src/microservices/tsconfig.json'),
websockets: ts.createProject('src/websockets/tsconfig.json'),
testing: ts.createProject('src/testing/tsconfig.json')
};
const modules = Object.keys(packages);
const source = 'src';
const dist = 'node_modules/@nestjs'
gulp.task('default', function () {
modules.forEach((module) => {
gulp.watch([`${source}/${module}/**/*.ts`, `${source}/${module}/*.ts`], [module]);
});
});
modules.forEach((module) => {
gulp.task(module, () => {
return packages[module].src()
.pipe(packages[module]())
.pipe(gulp.dest(`${dist}/${module}`));
});
});

View File

@@ -49,7 +49,7 @@
"@nestjs/common": "*",
"@nestjs/core": "*",
"@nestjs/microservices": "*",
"@nestjs/testing": "2.0.2",
"@nestjs/testing": "*",
"@nestjs/websockets": "*",
"cli-color": "^1.1.0",
"express": "^4.14.0",
@@ -63,7 +63,6 @@
"typescript": "^2.2.1"
},
"devDependencies": {
"@nestjs/common": "^1.0.0",
"@types/chai": "^3.4.34",
"@types/express": "^4.0.35",
"@types/mocha": "^2.2.38",
@@ -77,6 +76,9 @@
"concurrently": "^3.4.0",
"core-js": "^2.4.1",
"coveralls": "^2.11.16",
"gulp": "^3.9.1",
"gulp-typescript": "^3.1.6",
"gulp-watch": "^4.3.11",
"imports-loader": "^0.7.0",
"istanbul": "^0.4.5",
"json-loader": "^0.5.4",

View File

@@ -11,3 +11,4 @@ export * from './nest-microservice.interface';
export * from './modules/on-init.interface';
export * from './exceptions/exception-filter.interface';
export * from './middlewares';
export * from './socket-io-adapter.interface';

View File

@@ -1,10 +1,12 @@
import { MicroserviceConfiguration } from '@nestjs/microservices';
import { INestMicroservice } from './index';
import { SocketIoAdapter } from './socket-io-adapter.interface';
export interface INestApplication {
init(): void;
listen(port: number, callback?: () => void);
setGlobalPrefix(prefix: string): void;
setIoAdapter(adapter: SocketIoAdapter): void;
connectMicroservice(config: MicroserviceConfiguration): INestMicroservice;
getMicroservices(): INestMicroservice[];
startAllMicroservices(callback: () => void): void;

View File

@@ -0,0 +1,4 @@
export interface SocketIoAdapter {
create(port: number);
createWithNamespace(port: number, namespace: string);
}

View File

@@ -1,5 +1,9 @@
import { IoAdapter } from '@nestjs/websockets/adapters/io-adapter';
import { SocketIoAdapter } from '@nestjs/common/interfaces';
export class ApplicationConfig {
private globalPrefix = '';
private ioAdapter: SocketIoAdapter = IoAdapter as any;
public setGlobalPrefix(prefix: string) {
this.globalPrefix = prefix;
@@ -8,4 +12,12 @@ export class ApplicationConfig {
public getGlobalPrefix() {
return this.globalPrefix;
}
public setIoAdapter(ioAdapter: SocketIoAdapter) {
this.ioAdapter = ioAdapter;
}
public getIoAdapter(): SocketIoAdapter {
return this.ioAdapter;
}
}

View File

@@ -12,6 +12,7 @@ import { ApplicationConfig } from './application-config';
import { validatePath } from '@nestjs/common/utils/shared.utils';
import { MicroserviceConfiguration } from '@nestjs/microservices';
import { NestMicroservice } from './index';
import { SocketIoAdapter } from '@nestjs/common/interfaces';
export class NestApplication implements INestApplication {
private readonly config = new ApplicationConfig();
@@ -29,7 +30,7 @@ export class NestApplication implements INestApplication {
}
public setupModules() {
SocketModule.setup(this.container);
SocketModule.setup(this.container, this.config);
MiddlewaresModule.setup(this.container);
MicroservicesModule.setupClients(this.container);
}
@@ -79,6 +80,10 @@ export class NestApplication implements INestApplication {
this.config.setGlobalPrefix(prefix);
}
public setIoAdapter(adapter: SocketIoAdapter) {
this.config.setIoAdapter(adapter);
}
private setupMiddlewares(instance) {
MiddlewaresModule.setupMiddlewares(instance);
}

View File

@@ -6,14 +6,15 @@ import { WebSocketsController } from './web-sockets-controller';
import { Injectable } from '@nestjs/common/interfaces/injectable.interface';
import { SocketServerProvider } from './socket-server-provider';
import { GATEWAY_METADATA } from './constants';
import { ApplicationConfig } from '@nestjs/core/application-config';
export class SocketModule {
private static socketsContainer = new SocketsContainer();
private static webSocketsController: WebSocketsController;
public static setup(container) {
public static setup(container, config) {
this.webSocketsController = new WebSocketsController(
new SocketServerProvider(this.socketsContainer),
new SocketServerProvider(this.socketsContainer, config),
container,
);

View File

@@ -1,11 +1,13 @@
import { SocketsContainer } from './container';
import { ObservableSocket } from './observable-socket';
import { ObservableSocketServer } from './interfaces/observable-socket-server.interface';
import { IoAdapter } from './adapters/io-adapter';
import { validatePath } from '@nestjs/common/utils/shared.utils';
import { ApplicationConfig } from '@nestjs/core/application-config';
export class SocketServerProvider {
constructor(private readonly socketsContainer: SocketsContainer) {}
constructor(
private readonly socketsContainer: SocketsContainer,
private readonly applicationConfig: ApplicationConfig) {}
public scanForSocketServer(namespace: string, port: number): ObservableSocketServer {
const observableServer = this.socketsContainer.getServer(namespace, port);
@@ -21,10 +23,11 @@ export class SocketServerProvider {
}
private getServerOfNamespace(namespace: string, port: number) {
const adapter = this.applicationConfig.getIoAdapter();
if (namespace) {
return IoAdapter.createWithNamespace(port, this.validateNamespace(namespace));
return adapter.createWithNamespace(port, this.validateNamespace(namespace));
}
return IoAdapter.create(port);
return adapter.create(port);
}
private validateNamespace(namespace: string): string {