import { Module } from '@nestjs/common';
import { TestService } from './test.service';
import { TestGateway } from './test.gateway';
@Module({
providers: [TestGateway, TestService],
})
export class TestModule {}
import { WebSocketGateway, SubscribeMessage, MessageBody } from '@nestjs/websockets';
import { TestService } from './test.service';
import { CreateTestDto } from './dto/create-test.dto';
import { UpdateTestDto } from './dto/update-test.dto';
@WebSocketGateway(3002, {
cors: {
origin: process.env.CLIENT_URL,
methods: ["GET", "POST"],
allowedHeaders: ["Content-Type"],
credentials: true,
},
namespace: '/'
})export class TestGateway {
constructor(private readonly testService: TestService) {}
@SubscribeMessage('createTest')
create(@MessageBody() createTestDto: CreateTestDto) {
return this.testService.create(createTestDto);
}
@SubscribeMessage('findAllTest')
findAll() {
return this.testService.findAll();
}
@SubscribeMessage('findOneTest')
findOne(@MessageBody() id: number) {
return this.testService.findOne(id);
}
@SubscribeMessage('updateTest')
update(@MessageBody() updateTestDto: UpdateTestDto) {
return this.testService.update(updateTestDto.id, updateTestDto);
}
@SubscribeMessage('removeTest')
remove(@MessageBody() id: number) {
return this.testService.remove(id);
}
}
import { Injectable } from '@nestjs/common';
import { CreateTestDto } from './dto/create-test.dto';
import { UpdateTestDto } from './dto/update-test.dto';
import { WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';
@Injectable()
export class TestService {
@WebSocketServer()
private server: Server;
create(createTestDto: CreateTestDto) {
return 'This action adds a new test';
}
findAll() {
return this.server.emit('findAllTest', { body: 'hello' });
}
findOne(id: number) {
return `This action returns a #${id} test`;
}
update(id: number, updateTestDto: UpdateTestDto) {
return `This action updates a #${id} test`;
}
remove(id: number) {
return `This action removes a #${id} test`;
}
}