mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
Merge pull request #10545 from jmcdo29/feat/websocket-get-pattern
feat: create method on client to get websocket pattern
This commit is contained in:
@@ -66,5 +66,21 @@ describe('WebSocketGateway', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it(`should be able to get the pattern in an interceptor`, async () => {
|
||||
app = await createNestApp(ApplicationGateway);
|
||||
await app.listen(3000);
|
||||
|
||||
ws = io('http://localhost:8080');
|
||||
ws.emit('getClient', {
|
||||
test: 'test',
|
||||
});
|
||||
await new Promise<void>(resolve =>
|
||||
ws.on('popClient', data => {
|
||||
expect(data.path).to.be.eql('getClient');
|
||||
resolve();
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => app.close());
|
||||
});
|
||||
|
||||
@@ -194,6 +194,30 @@ describe('WebSocketGateway (WsAdapter)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should let the execution context have a getPattern() method on getClient()', async () => {
|
||||
app = await createNestApp(ApplicationGateway);
|
||||
await app.listen(3000);
|
||||
|
||||
ws = new WebSocket('ws://localhost:8080');
|
||||
await new Promise(resolve => ws.on('open', resolve));
|
||||
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
event: 'getClient',
|
||||
data: {
|
||||
test: 'test',
|
||||
},
|
||||
}),
|
||||
);
|
||||
await new Promise<void>(resolve =>
|
||||
ws.on('message', data => {
|
||||
expect(JSON.parse(data).data.path).to.be.eql('getClient');
|
||||
ws.close();
|
||||
resolve();
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(async function () {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { UseInterceptors } from '@nestjs/common';
|
||||
import {
|
||||
MessageBody,
|
||||
SubscribeMessage,
|
||||
WebSocketGateway,
|
||||
} from '@nestjs/websockets';
|
||||
import { RequestInterceptor } from './request.interceptor';
|
||||
|
||||
@WebSocketGateway(8080)
|
||||
export class ApplicationGateway {
|
||||
@@ -13,4 +15,13 @@ export class ApplicationGateway {
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
@UseInterceptors(RequestInterceptor)
|
||||
@SubscribeMessage('getClient')
|
||||
getPathCalled(client, data) {
|
||||
return {
|
||||
event: 'popClient',
|
||||
data: { ...data, path: client.pattern },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
11
integration/websockets/src/request.interceptor.ts
Normal file
11
integration/websockets/src/request.interceptor.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { CallHandler, ExecutionContext, Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class RequestInterceptor {
|
||||
intercept(context: ExecutionContext, next: CallHandler) {
|
||||
const client = context.switchToWs().getClient();
|
||||
const pattern = context.switchToWs().getPattern();
|
||||
client.pattern = pattern;
|
||||
return next.handle();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
import { UseInterceptors } from '@nestjs/common';
|
||||
import { SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';
|
||||
import { RequestInterceptor } from './request.interceptor';
|
||||
|
||||
@WebSocketGateway()
|
||||
export class ServerGateway {
|
||||
@@ -9,4 +11,13 @@ export class ServerGateway {
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
@UseInterceptors(RequestInterceptor)
|
||||
@SubscribeMessage('getClient')
|
||||
getPathCalled(client, data) {
|
||||
return {
|
||||
event: 'popClient',
|
||||
data: { ...data, path: client.pattern },
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user