fix(websockets): allow to share a single server with multiple paths (ws)

This commit is contained in:
Kamil Myśliwiec
2021-02-04 14:48:59 +01:00
parent a138be8c0e
commit d381e149ca
9 changed files with 230 additions and 16 deletions

View File

@@ -5,7 +5,10 @@ import { expect } from 'chai';
import * as WebSocket from 'ws';
import { ApplicationGateway } from '../src/app.gateway';
import { CoreGateway } from '../src/core.gateway';
import { ExamplePathGateway } from '../src/example-path.gateway';
import { ServerGateway } from '../src/server.gateway';
import { WsPathGateway } from '../src/ws-path.gateway';
import { WsPathGateway2 } from '../src/ws-path2.gateway';
async function createNestApp(...gateways): Promise<INestApplication> {
const testingModule = await Test.createTestingModule({
@@ -65,7 +68,81 @@ describe('WebSocketGateway (WsAdapter)', () => {
);
});
it(`should support 2 different gateways`, async function () {
it(`should handle message on a different path`, async () => {
app = await createNestApp(WsPathGateway);
await app.listenAsync(3000);
try {
ws = new WebSocket('ws://localhost:3000/ws-path');
await new Promise((resolve, reject) => {
ws.on('open', resolve);
ws.on('error', reject);
});
ws.send(
JSON.stringify({
event: 'push',
data: {
test: 'test',
},
}),
);
await new Promise<void>(resolve =>
ws.on('message', data => {
expect(JSON.parse(data).data.test).to.be.eql('test');
resolve();
}),
);
} catch (err) {
console.log(err);
}
});
it(`should support 2 different gateways running on different paths`, async function () {
this.retries(10);
app = await createNestApp(ExamplePathGateway, WsPathGateway2);
await app.listenAsync(3000);
// open websockets delay
await new Promise(resolve => setTimeout(resolve, 1000));
ws = new WebSocket('ws://localhost:8082/example');
ws2 = new WebSocket('ws://localhost:8082/ws-path');
await new Promise<void>(resolve =>
ws.on('open', () => {
ws.on('message', data => {
expect(JSON.parse(data).data.test).to.be.eql('test');
resolve();
});
ws.send(
JSON.stringify({
event: 'push',
data: {
test: 'test',
},
}),
);
}),
);
await new Promise<void>(resolve => {
ws2.on('message', data => {
expect(JSON.parse(data).data.test).to.be.eql('test');
resolve();
});
ws2.send(
JSON.stringify({
event: 'push',
data: {
test: 'test',
},
}),
);
});
});
it(`should support 2 different gateways running on the same path (but different ports)`, async function () {
this.retries(10);
app = await createNestApp(ApplicationGateway, CoreGateway);

View File

@@ -0,0 +1,14 @@
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';
@WebSocketGateway(8082, {
path: '/example',
})
export class ExamplePathGateway {
@SubscribeMessage('push')
onPush(client, data) {
return {
event: 'pop',
data,
};
}
}

View File

@@ -0,0 +1,14 @@
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';
@WebSocketGateway({
path: '/ws-path',
})
export class WsPathGateway {
@SubscribeMessage('push')
onPush(client, data) {
return {
event: 'pop',
data,
};
}
}

View File

@@ -0,0 +1,14 @@
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';
@WebSocketGateway(8082, {
path: '/ws-path',
})
export class WsPathGateway2 {
@SubscribeMessage('push')
onPush(client, data) {
return {
event: 'pop',
data,
};
}
}