wip chat in room, now broadcast with name
This commit is contained in:
@@ -35,9 +35,6 @@ import { ChatModule } from './chat/chat.module';
|
||||
// GameModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [
|
||||
AppService,
|
||||
ChatGateway,
|
||||
],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@@ -3,12 +3,15 @@ import {
|
||||
SubscribeMessage,
|
||||
WebSocketServer,
|
||||
MessageBody,
|
||||
ConnectedSocket,
|
||||
OnGatewayConnection,
|
||||
OnGatewayDisconnect,
|
||||
} from '@nestjs/websockets';
|
||||
import { UseGuards } from '@nestjs/common';
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
|
||||
import { ChatService } from 'src/chat/chat.service';
|
||||
import { ChatService } from './chat.service';
|
||||
import { AuthenticateGuard } from 'src/auth/42/guards/42guards';
|
||||
|
||||
@WebSocketGateway(5000, {
|
||||
path: '/chat',
|
||||
@@ -29,22 +32,25 @@ export class ChatGateway
|
||||
// how to guard the handleConnection ?
|
||||
// https://github.com/nestjs/nest/issues/882
|
||||
async handleConnection(client) {
|
||||
console.log('---- Client connected :', client.id);
|
||||
console.log('- Client connected :', client.id, client.handshake.query.username);
|
||||
client.username = client.handshake.query.username;
|
||||
}
|
||||
async handleDisconnect(client) {
|
||||
console.log('---- client disconnected :', client.id);
|
||||
console.log('- Client disconnected :', client.id, client.username);
|
||||
}
|
||||
|
||||
// @UseGuards(AuthenticateGuard)
|
||||
@SubscribeMessage('message')
|
||||
handleMessage(@MessageBody() message: string): void {
|
||||
console.log('-------- msg --------------');
|
||||
console.log(message);
|
||||
// handleMessage(@MessageBody() message: string): void {
|
||||
// handleMessage(client: any, message: string): void {
|
||||
handleMessage(@ConnectedSocket() client, @MessageBody() message: string): void{
|
||||
// const paginationQuery = new PaginationQueryDto();
|
||||
// const users = this.usersService.findAll(paginationQuery);
|
||||
// console.log('users :', users);
|
||||
// this.server.emit('message', message);
|
||||
this.chatService.add_message(this.server, message);
|
||||
console.log('client.username :', client.username);
|
||||
client.local.emit('message', client.username, message);
|
||||
// this.chatService.add_message(this.server, message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,22 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ChatController } from './chat.controller';
|
||||
import { ChatService } from './chat.service';
|
||||
import { ChatGateway } from './chat.gateway';
|
||||
import { UsersModule } from 'src/users/users.module';
|
||||
|
||||
@Module({
|
||||
providers: [ChatService],
|
||||
controllers: [ChatController],
|
||||
exports: [ChatService],
|
||||
imports: [
|
||||
UsersModule,
|
||||
],
|
||||
controllers: [
|
||||
ChatController,
|
||||
],
|
||||
exports: [
|
||||
],
|
||||
providers: [
|
||||
ChatService,
|
||||
ChatGateway,
|
||||
],
|
||||
})
|
||||
|
||||
export class ChatModule {}
|
||||
|
||||
Reference in New Issue
Block a user