added socket dto
This commit is contained in:
@@ -12,34 +12,16 @@ import { ChatGateway } from './chat.gateway';
|
|||||||
@Controller('chat')
|
@Controller('chat')
|
||||||
export class ChatController {
|
export class ChatController {
|
||||||
|
|
||||||
private allowed_chars: string;
|
private allowed_chars = '-#!?_';
|
||||||
|
private escape_chars(str)
|
||||||
|
{
|
||||||
|
return str.split("").join("\\");
|
||||||
|
}
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private chatService: ChatService,
|
private chatService: ChatService,
|
||||||
private chatGateway: ChatGateway,
|
private chatGateway: ChatGateway,
|
||||||
)
|
) {}
|
||||||
{
|
|
||||||
this.allowed_chars = '-#!?_';
|
|
||||||
/*
|
|
||||||
let text = "The best things in life are-* free";
|
|
||||||
|
|
||||||
function escape_chars(str)
|
|
||||||
{
|
|
||||||
return new_str = str.split("").join("\\");
|
|
||||||
}
|
|
||||||
|
|
||||||
let allowed_chars = '#!?\\-_';
|
|
||||||
let regex_base = `[a-zA-Z0-9\\s${allowed_chars}]`;
|
|
||||||
let test_regex = new RegExp(`^${regex_base}+$`);
|
|
||||||
|
|
||||||
let result = "";
|
|
||||||
result = escape_chars(allowed_chars);
|
|
||||||
//if (test_regex.test(text) === false)
|
|
||||||
// result = text.replace(new RegExp(regex_base, "g"), "");
|
|
||||||
|
|
||||||
document.getElementById("demo").innerHTML = "[" + result + "]";
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@@ -92,7 +74,8 @@ export class ChatController {
|
|||||||
{
|
{
|
||||||
console.log("- in createRoom controller");
|
console.log("- in createRoom controller");
|
||||||
|
|
||||||
let regex_base = `[a-zA-Z0-9\\s${this.allowed_chars}]`;
|
let chars = this.escape_chars(this.allowed_chars);
|
||||||
|
let regex_base = `[a-zA-Z0-9\\s${chars}]`;
|
||||||
let test_regex = new RegExp(`^${regex_base}+$`);
|
let test_regex = new RegExp(`^${regex_base}+$`);
|
||||||
if (test_regex.test(createRoomDto.room_name) === false)
|
if (test_regex.test(createRoomDto.room_name) === false)
|
||||||
{
|
{
|
||||||
@@ -113,9 +96,6 @@ export class ChatController {
|
|||||||
console.log("- in joinRoom controller");
|
console.log("- in joinRoom controller");
|
||||||
console.log("-- room_name", joinRoomDto.room_name);
|
console.log("-- room_name", joinRoomDto.room_name);
|
||||||
const response = await this.chatService.addUserToRoom(req.user.username, joinRoomDto.room_name);
|
const response = await this.chatService.addUserToRoom(req.user.username, joinRoomDto.room_name);
|
||||||
|
|
||||||
//this.chatGateway.joinRoom(null, joinRoomDto.room_name);
|
|
||||||
|
|
||||||
console.log("- out joinRoom controller");
|
console.log("- out joinRoom controller");
|
||||||
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: response });
|
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: response });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { WebSocketGateway, SubscribeMessage, WebSocketServer, MessageBody, ConnectedSocket, OnGatewayConnection, OnGatewayDisconnect } from '@nestjs/websockets';
|
import { WebSocketGateway, SubscribeMessage, WebSocketServer, MessageBody, ConnectedSocket, OnGatewayConnection, OnGatewayDisconnect } from '@nestjs/websockets';
|
||||||
import { UsersService } from 'src/users/users.service';
|
import { UsersService } from 'src/users/users.service';
|
||||||
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
|
|
||||||
import { ChatService } from './chat.service';
|
import { ChatService } from './chat.service';
|
||||||
|
import { socketDto } from './dto/socket.dto';
|
||||||
|
|
||||||
@WebSocketGateway(5000, {
|
@WebSocketGateway(5000, {
|
||||||
path: '/chat',
|
path: '/chat',
|
||||||
@@ -19,19 +19,16 @@ export class ChatGateway
|
|||||||
@WebSocketServer()
|
@WebSocketServer()
|
||||||
server;
|
server;
|
||||||
|
|
||||||
|
async handleConnection(client: socketDto) {
|
||||||
// how to guard the handleConnection ?
|
|
||||||
// https://github.com/nestjs/nest/issues/882
|
|
||||||
async handleConnection(client) {
|
|
||||||
console.log('- Client connected :', client.id, client.handshake.query.username);
|
console.log('- Client connected :', client.id, client.handshake.query.username);
|
||||||
client.username = client.handshake.query.username;
|
client.username = client.handshake.query.username.toString();
|
||||||
}
|
}
|
||||||
async handleDisconnect(client) {
|
async handleDisconnect(client: socketDto) {
|
||||||
console.log('- Client disconnected :', client.id, client.username);
|
console.log('- Client disconnected :', client.id, client.username);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeMessage('join')
|
@SubscribeMessage('join')
|
||||||
async joinRoom(@ConnectedSocket() socket, @MessageBody() room_name: string): Promise<void>
|
async joinRoom(@ConnectedSocket() socket: socketDto, @MessageBody() room_name: string): Promise<void>
|
||||||
{
|
{
|
||||||
console.log('- in joinRoom gateway');
|
console.log('- in joinRoom gateway');
|
||||||
socket.leave(socket.room);
|
socket.leave(socket.room);
|
||||||
@@ -45,7 +42,7 @@ export class ChatGateway
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeMessage('change')
|
@SubscribeMessage('change')
|
||||||
async changeRoom(@ConnectedSocket() socket, @MessageBody() room_name: string): Promise<void>
|
async changeRoom(@ConnectedSocket() socket: socketDto, @MessageBody() room_name: string): Promise<void>
|
||||||
{
|
{
|
||||||
console.log('- in changeRoom gateway');
|
console.log('- in changeRoom gateway');
|
||||||
socket.leave(socket.room);
|
socket.leave(socket.room);
|
||||||
@@ -56,10 +53,9 @@ export class ChatGateway
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeMessage('message')
|
@SubscribeMessage('message')
|
||||||
async handleMessage(@ConnectedSocket() socket, @MessageBody() message: string): Promise<void>
|
async handleMessage(@ConnectedSocket() socket: socketDto, @MessageBody() message: string): Promise<void>
|
||||||
{
|
{
|
||||||
console.log('- in handleMessage gateway');
|
console.log('- in handleMessage gateway');
|
||||||
//let room_name = await this.chatService.getCurrentRoom(socket.username);
|
|
||||||
socket.to(socket.room).emit('message', socket.username, message);
|
socket.to(socket.room).emit('message', socket.username, message);
|
||||||
let room_name = await this.chatService.getCurrentRoomName(socket.username);
|
let room_name = await this.chatService.getCurrentRoomName(socket.username);
|
||||||
await this.chatService.addMessageToRoom(room_name, socket.username, message);
|
await this.chatService.addMessageToRoom(room_name, socket.username, message);
|
||||||
|
|||||||
@@ -228,5 +228,26 @@ export class ChatService {
|
|||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* GATEWAY EVENTS *****************************************
|
||||||
|
|
||||||
|
async handleMessage()
|
||||||
|
{
|
||||||
|
console.log("-- in getUserByName service");
|
||||||
|
|
||||||
|
// socket.to(socket.room).emit('message', socket.username, message);
|
||||||
|
// let room_name = await this.chatService.getCurrentRoomName(socket.username);
|
||||||
|
// await this.chatService.addMessageToRoom(room_name, socket.username, message);
|
||||||
|
|
||||||
|
const user = await this.userRepository
|
||||||
|
.createQueryBuilder('user')
|
||||||
|
.where('user.username = :name', { name: username })
|
||||||
|
.getOne();
|
||||||
|
|
||||||
|
console.log("-- out getUserByName service");
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
srcs/requirements/nestjs/api_back/src/chat/dto/socket.dto.ts
Normal file
13
srcs/requirements/nestjs/api_back/src/chat/dto/socket.dto.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator";
|
||||||
|
import { Socket } from 'socket.io';
|
||||||
|
|
||||||
|
export class socketDto extends Socket
|
||||||
|
{
|
||||||
|
@IsString()
|
||||||
|
username: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
room: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user