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) ## 2.0.3 (15.05.2017)
**@nestjs/common** **@nestjs/common**
- `Req()` (`Request()`) and `Res()` (`Response()`) aliases to avoid conflicts with express typings - `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 { NestFactory } from './../src/core';
import { ApplicationModule } from './modules/app.module'; import { ApplicationModule } from './modules/app.module';
import { Transport } from '../src/microservices/index'; import { Transport } from '../src/microservices/index';
@@ -8,7 +9,14 @@ const microservice = app.connectMicroservice({
transport: Transport.TCP, transport: Transport.TCP,
port: 5667, 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.startAllMicroservices(() => console.log('All microservices are listening...'));
app.listen(port, () => { app.listen(port, () => {
console.log('Application listen on port:', 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/common": "*",
"@nestjs/core": "*", "@nestjs/core": "*",
"@nestjs/microservices": "*", "@nestjs/microservices": "*",
"@nestjs/testing": "2.0.2", "@nestjs/testing": "*",
"@nestjs/websockets": "*", "@nestjs/websockets": "*",
"cli-color": "^1.1.0", "cli-color": "^1.1.0",
"express": "^4.14.0", "express": "^4.14.0",
@@ -63,7 +63,6 @@
"typescript": "^2.2.1" "typescript": "^2.2.1"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/common": "^1.0.0",
"@types/chai": "^3.4.34", "@types/chai": "^3.4.34",
"@types/express": "^4.0.35", "@types/express": "^4.0.35",
"@types/mocha": "^2.2.38", "@types/mocha": "^2.2.38",
@@ -77,6 +76,9 @@
"concurrently": "^3.4.0", "concurrently": "^3.4.0",
"core-js": "^2.4.1", "core-js": "^2.4.1",
"coveralls": "^2.11.16", "coveralls": "^2.11.16",
"gulp": "^3.9.1",
"gulp-typescript": "^3.1.6",
"gulp-watch": "^4.3.11",
"imports-loader": "^0.7.0", "imports-loader": "^0.7.0",
"istanbul": "^0.4.5", "istanbul": "^0.4.5",
"json-loader": "^0.5.4", "json-loader": "^0.5.4",

View File

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

View File

@@ -1,10 +1,12 @@
import { MicroserviceConfiguration } from '@nestjs/microservices'; import { MicroserviceConfiguration } from '@nestjs/microservices';
import { INestMicroservice } from './index'; import { INestMicroservice } from './index';
import { SocketIoAdapter } from './socket-io-adapter.interface';
export interface INestApplication { export interface INestApplication {
init(): void; init(): void;
listen(port: number, callback?: () => void); listen(port: number, callback?: () => void);
setGlobalPrefix(prefix: string): void; setGlobalPrefix(prefix: string): void;
setIoAdapter(adapter: SocketIoAdapter): void;
connectMicroservice(config: MicroserviceConfiguration): INestMicroservice; connectMicroservice(config: MicroserviceConfiguration): INestMicroservice;
getMicroservices(): INestMicroservice[]; getMicroservices(): INestMicroservice[];
startAllMicroservices(callback: () => void): void; 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 { export class ApplicationConfig {
private globalPrefix = ''; private globalPrefix = '';
private ioAdapter: SocketIoAdapter = IoAdapter as any;
public setGlobalPrefix(prefix: string) { public setGlobalPrefix(prefix: string) {
this.globalPrefix = prefix; this.globalPrefix = prefix;
@@ -8,4 +12,12 @@ export class ApplicationConfig {
public getGlobalPrefix() { public getGlobalPrefix() {
return this.globalPrefix; 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 { validatePath } from '@nestjs/common/utils/shared.utils';
import { MicroserviceConfiguration } from '@nestjs/microservices'; import { MicroserviceConfiguration } from '@nestjs/microservices';
import { NestMicroservice } from './index'; import { NestMicroservice } from './index';
import { SocketIoAdapter } from '@nestjs/common/interfaces';
export class NestApplication implements INestApplication { export class NestApplication implements INestApplication {
private readonly config = new ApplicationConfig(); private readonly config = new ApplicationConfig();
@@ -29,7 +30,7 @@ export class NestApplication implements INestApplication {
} }
public setupModules() { public setupModules() {
SocketModule.setup(this.container); SocketModule.setup(this.container, this.config);
MiddlewaresModule.setup(this.container); MiddlewaresModule.setup(this.container);
MicroservicesModule.setupClients(this.container); MicroservicesModule.setupClients(this.container);
} }
@@ -79,6 +80,10 @@ export class NestApplication implements INestApplication {
this.config.setGlobalPrefix(prefix); this.config.setGlobalPrefix(prefix);
} }
public setIoAdapter(adapter: SocketIoAdapter) {
this.config.setIoAdapter(adapter);
}
private setupMiddlewares(instance) { private setupMiddlewares(instance) {
MiddlewaresModule.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 { Injectable } from '@nestjs/common/interfaces/injectable.interface';
import { SocketServerProvider } from './socket-server-provider'; import { SocketServerProvider } from './socket-server-provider';
import { GATEWAY_METADATA } from './constants'; import { GATEWAY_METADATA } from './constants';
import { ApplicationConfig } from '@nestjs/core/application-config';
export class SocketModule { export class SocketModule {
private static socketsContainer = new SocketsContainer(); private static socketsContainer = new SocketsContainer();
private static webSocketsController: WebSocketsController; private static webSocketsController: WebSocketsController;
public static setup(container) { public static setup(container, config) {
this.webSocketsController = new WebSocketsController( this.webSocketsController = new WebSocketsController(
new SocketServerProvider(this.socketsContainer), new SocketServerProvider(this.socketsContainer, config),
container, container,
); );

View File

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