Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(websockets/platform-ws): support ws servers running on different paths #6321

Merged
merged 8 commits into from
Feb 5, 2021
79 changes: 78 additions & 1 deletion integration/websockets/e2e/ws-gateway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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:3000/example');
ws2 = new WebSocket('ws://localhost:3000/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);
Expand Down
14 changes: 14 additions & 0 deletions integration/websockets/src/example-path.gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';

@WebSocketGateway({
path: '/example',
})
export class ExamplePathGateway {
@SubscribeMessage('push')
onPush(client, data) {
return {
event: 'pop',
data,
};
}
}
14 changes: 14 additions & 0 deletions integration/websockets/src/ws-path.gateway.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
}
14 changes: 14 additions & 0 deletions integration/websockets/src/ws-path2.gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';

@WebSocketGateway({
path: '/ws-path',
})
export class WsPathGateway2 {
@SubscribeMessage('push')
onPush(client, data) {
return {
event: 'pop',
data,
};
}
}
Loading