-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tcp): implements first draft of tcp parser
The TCP parser pulls information from the socket connection and has a hard codeded method of TCP, though this may change to protocol instead of method. Protocol currently takes from the remote connections family (an IP version). The call point must be stringified in the case of using a json as the message pattern (as the NestJS docs show). Integration has also been built for this, using concurrently to run the client and server servers. fix #23
- Loading branch information
Showing
16 changed files
with
6,406 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "@ogma/platform-socket-io-integration", | ||
"version": "0.0.0", | ||
"scripts": { | ||
"prebuild": "rimraf dist", | ||
"build": "tsc -p tsconfig.build.json", | ||
"start": "concurrently \"yarn start:server\" \"yarn start:client\"", | ||
"start:client": "node dist/client/main.js", | ||
"start:server": "node dist/server/main.js", | ||
"visual": "node testScript" | ||
}, | ||
"private": true, | ||
"dependencies": { | ||
"@nestjs/common": "^7.0.0", | ||
"@nestjs/core": "^7.0.0", | ||
"@nestjs/microservices": "^7.0.0", | ||
"@nestjs/platform-express": "^7.0.0" | ||
}, | ||
"devDependencies": { | ||
"@nestjs/cli": "^7.1.1", | ||
"@nestjs/testing": "^7.0.0", | ||
"concurrently": "^5.1.0", | ||
"jest": "^25.2.3", | ||
"reflect-metadata": "0.1.13", | ||
"rimraf": "^3.0.2", | ||
"rxjs": "6.5.4", | ||
"ts-jest": "^25.2.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { AppService } from './app.service'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
|
||
@Get() | ||
getMessage() { | ||
return this.appService.getHello(); | ||
} | ||
|
||
@Get('error') | ||
getError() { | ||
return this.appService.getError(); | ||
} | ||
|
||
@Get('skip') | ||
getSkip() { | ||
return this.appService.getSkip(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ClientsModule, Transport } from '@nestjs/microservices'; | ||
import { OgmaModule } from '@ogma/nestjs-module'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
ClientsModule.register([ | ||
{ | ||
name: 'CLIENT_SERVICE', | ||
transport: Transport.TCP, | ||
options: { port: 3001 }, | ||
}, | ||
]), | ||
OgmaModule.forRoot({ | ||
interceptor: false, | ||
}), | ||
], | ||
providers: [AppService], | ||
controllers: [AppController], | ||
}) | ||
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Inject, Injectable, OnApplicationBootstrap } from '@nestjs/common'; | ||
import { ClientProxy } from '@nestjs/microservices'; | ||
import { Observable } from 'rxjs'; | ||
|
||
@Injectable() | ||
export class AppService implements OnApplicationBootstrap { | ||
constructor(@Inject('CLIENT_SERVICE') private readonly client: ClientProxy) {} | ||
|
||
async onApplicationBootstrap() { | ||
await this.client.connect(); | ||
} | ||
|
||
getHello(): Observable<string> { | ||
return this.client.send<string>({ cmd: 'message' }, ''); | ||
} | ||
|
||
getError(): Observable<any> { | ||
return this.client.send({ cmd: 'error' }, ''); | ||
} | ||
|
||
getSkip(): Observable<string> { | ||
return this.client.send({ cmd: 'skip' }, ''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { AppModule } from './app.module'; | ||
import { OgmaService } from '@ogma/nestjs-module'; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(AppModule); | ||
const logger = app.get(OgmaService); | ||
await app.listen(3000); | ||
logger.log(`TCP Microservice client listening at ${await app.getUrl()}`); | ||
} | ||
|
||
bootstrap(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { | ||
BadRequestException, | ||
Controller, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { MessagePattern } from '@nestjs/microservices'; | ||
import { OgmaInterceptor, OgmaSkip } from '@ogma/nestjs-module'; | ||
import { AppService } from './app.service'; | ||
|
||
@UseInterceptors(OgmaInterceptor) | ||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
|
||
@MessagePattern({ cmd: 'message' }) | ||
getMessage() { | ||
return this.appService.getHello(); | ||
} | ||
|
||
@MessagePattern({ cmd: 'error' }) | ||
getError() { | ||
throw new BadRequestException('Borked'); | ||
} | ||
|
||
@OgmaSkip() | ||
@MessagePattern({ cmd: 'skip' }) | ||
getSkip() { | ||
return this.appService.getHello(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { OgmaModule } from '@ogma/nestjs-module'; | ||
import { TcpParser } from '@ogma/platform-tcp'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
OgmaModule.forRoot({ | ||
interceptor: { | ||
rpc: TcpParser, | ||
}, | ||
}), | ||
], | ||
providers: [AppService], | ||
controllers: [AppController], | ||
}) | ||
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
getHello() { | ||
return 'Hello World!'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { MicroserviceOptions, Transport } from '@nestjs/microservices'; | ||
import { OgmaService } from '@ogma/nestjs-module'; | ||
import { AppModule } from './app.module'; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.createMicroservice<MicroserviceOptions>( | ||
AppModule, | ||
{ | ||
transport: Transport.TCP, | ||
options: { | ||
port: 3001, | ||
}, | ||
}, | ||
); | ||
const logger = app.get(OgmaService); | ||
await app.listenAsync(); | ||
logger.log(`Microservice server listening at port 3001`); | ||
} | ||
|
||
bootstrap(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const { request } = require('http'); | ||
|
||
const baseUrl = 'http://localhost:3000/'; | ||
|
||
function cb(res) { | ||
let data = ''; | ||
res.on('data', (chunk) => (data += chunk)); | ||
res.on('end', () => console.log(data)); | ||
} | ||
|
||
function makeCall(url) { | ||
request(baseUrl + url, cb).end(); | ||
} | ||
|
||
makeCall(''); | ||
makeCall('error'); | ||
makeCall('skip'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "./tsconfig.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
Oops, something went wrong.