Skip to content

Commit

Permalink
feat(socket.io): implements parser for socket.io
Browse files Browse the repository at this point in the history
The SocketioInterceptorParser has two hardcoded values, one for method and one for protocol. It also
only has the concept of pass (200) and fail (500), though there could be a case for checking if the
error is an `HttpException` and if so getting the status from there like is done in the HttpParsers.

fix #22
  • Loading branch information
jmcdo29 committed Mar 28, 2020
1 parent 9811255 commit 6da8fdb
Show file tree
Hide file tree
Showing 14 changed files with 6,624 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ To build your the project and link the dependencies together you can run `lerna

When running the integration specific applications in a non--testing context (i.e. starting them locally), make sure to `cd` into the directory, `yarn install` the dependencies, and then run `yarn build && yarn start` to compile and start the server. From there, in a separate terminal, you can use [curl](https://curl.haxx.se/) or [Postman](https://www.postman.com/), or just use the browser directly. For websocket testing, you can use the node REPL (i.e. use `node` from the terminal) and make websocket calls from there.

## Commits

We are using [Conventional Commit]() to help keep commit messages aligned as development continues. The easiest way to get acquainted with what the commit should look like is to run `yarn commit` which will use the `git-cz` cli and walk you through the steps of committing. When asked about the scope, you can checkout the [commitlint.config](./commitlint.config.js) file and look at the `scope-enum` property. Once you've made your commit, prettier and eslint will run and ensure that the new code is up to the standards we have in place.

## Issues

Please raise an issue, or discuss with me [via email](mailto:me@jaymcdoniel.dev) or [Discord](https://discordapp.com) (PerfectOrphan31#6003) before opening a Pull Request so we can see if they align with the goals of the project.
28 changes: 28 additions & 0 deletions integration/socket.io/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@ogma/platform-socket-io-integration",
"version": "0.0.0",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"start": "nest start"
},
"private": true,
"dependencies": {
"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"@nestjs/platform-express": "^7.0.0",
"@nestjs/platform-socket.io": "^7.0.0",
"@nestjs/websockets": "^7.0.6"
},
"devDependencies": {
"@nestjs/cli": "^7.1.1",
"@nestjs/testing": "^7.0.0",
"@types/socket.io": "1.4.33",
"jest": "^25.2.3",
"reflect-metadata": "0.1.13",
"rimraf": "^3.0.2",
"rxjs": "6.5.4",
"socket.io": "^2.3.0",
"ts-jest": "^25.2.1"
}
}
26 changes: 26 additions & 0 deletions integration/socket.io/src/app.gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';
import { AppService } from './app.service';
import { BadRequestException, UseInterceptors } from '@nestjs/common';
import { OgmaInterceptor, OgmaSkip } from '@ogma/nestjs-module';

@UseInterceptors(OgmaInterceptor)
@WebSocketGateway()
export class AppGateway {
constructor(private readonly appService: AppService) {}

@SubscribeMessage('message')
getMessage(): string {
return this.appService.getHello();
}

@SubscribeMessage('throw')
getThrow(): never {
throw new BadRequestException('Borked');
}

@SubscribeMessage('skip')
@OgmaSkip()
getSkip(): string {
return this.appService.getHello();
}
}
17 changes: 17 additions & 0 deletions integration/socket.io/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Module } from '@nestjs/common';
import { OgmaModule } from '@ogma/nestjs-module';
import { SocketioInterceptorParser } from '@ogma/platform-socket.io';
import { AppGateway } from './app.gateway';
import { AppService } from './app.service';

@Module({
imports: [
OgmaModule.forRoot({
interceptor: {
ws: SocketioInterceptorParser,
},
}),
],
providers: [AppGateway, AppService],
})
export class AppModule {}
8 changes: 8 additions & 0 deletions integration/socket.io/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}
12 changes: 12 additions & 0 deletions integration/socket.io/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NestFactory } from '@nestjs/core';
import { OgmaService } from '@ogma/nestjs-module';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const logger = app.get(OgmaService);
await app.listen(process.env.PORT || 3000);
logger.log(`Socket.io test running at ${await app.getUrl()}`);
}

bootstrap();
16 changes: 16 additions & 0 deletions integration/socket.io/testScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const socket = require('socket.io-client')('http://[::1]:3000');

function cb(...args) {
args.forEach((arg) => console.log(arg));
}

function socketCall(message) {
socket.emit(message, '', cb);
}

socketCall('message');
socketCall('throw');
socketCall('skip');

setTimeout(() => socket.disconnect(), 500);
3 changes: 3 additions & 0 deletions integration/socket.io/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.json"
}
9 changes: 9 additions & 0 deletions integration/socket.io/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": false,
"rootDir": "./src",
"outDir": "./dist"
},
"include": ["./src"]
}
Loading

0 comments on commit 6da8fdb

Please sign in to comment.