socket and server are now in separate files
This commit is contained in:
@@ -263,3 +263,8 @@
|
|||||||
- user join empty protected room
|
- user join empty protected room
|
||||||
- is admin ?
|
- is admin ?
|
||||||
|
|
||||||
|
### pbms:
|
||||||
|
- leave room > automatic redirect to home : !see the room! && !can go inside! && !write sockets works but it's not saved in database!
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ import { ChatGateway } from './chat.gateway';
|
|||||||
import { Chatroom } from './entities/chatroom.entity';
|
import { Chatroom } from './entities/chatroom.entity';
|
||||||
import { socketDto } from './dto/socket.dto';
|
import { socketDto } from './dto/socket.dto';
|
||||||
import { roomDto } from './dto/room.dto';
|
import { roomDto } from './dto/room.dto';
|
||||||
import { printCaller } from './dev/dev_utils';
|
|
||||||
import { setCurrentRoomDto } from './dto/setCurrentRoom.dto';
|
import { setCurrentRoomDto } from './dto/setCurrentRoom.dto';
|
||||||
import { muteDto } from './dto/mute.dto';
|
import { muteDto } from './dto/mute.dto';
|
||||||
|
import { printCaller, printCallerError } from './dev/dev_utils';
|
||||||
|
|
||||||
@Controller('chat')
|
@Controller('chat')
|
||||||
export class ChatController {
|
export class ChatController {
|
||||||
@@ -52,7 +52,6 @@ export class ChatController {
|
|||||||
|
|
||||||
let fields = ["name", "type", "users", "protection", "allowed_users"];
|
let fields = ["name", "type", "users", "protection", "allowed_users"];
|
||||||
const rooms = await this.chatService.getMyRooms(req.user.username, fields);
|
const rooms = await this.chatService.getMyRooms(req.user.username, fields);
|
||||||
console.log("rooms:", rooms);
|
|
||||||
const blocked = await this.chatService.getListBlockUser(req.user.username);
|
const blocked = await this.chatService.getListBlockUser(req.user.username);
|
||||||
|
|
||||||
let filtered_rooms = rooms.filter(room =>
|
let filtered_rooms = rooms.filter(room =>
|
||||||
@@ -466,7 +465,6 @@ export class ChatController {
|
|||||||
|
|
||||||
const messages = await this.chatService.getMessagesFromCurrentRoom(req.user.username);
|
const messages = await this.chatService.getMessagesFromCurrentRoom(req.user.username);
|
||||||
|
|
||||||
console.log("messages:", messages);
|
|
||||||
const blocked = await this.chatService.getListBlockUser(req.user.username);
|
const blocked = await this.chatService.getListBlockUser(req.user.username);
|
||||||
let filtered_messages = messages.filter(message =>
|
let filtered_messages = messages.filter(message =>
|
||||||
{
|
{
|
||||||
@@ -508,11 +506,9 @@ export class ChatController {
|
|||||||
new_user.isadmin = true;
|
new_user.isadmin = true;
|
||||||
if (blocked.includes(username))
|
if (blocked.includes(username))
|
||||||
new_user.isblocked = true;
|
new_user.isblocked = true;
|
||||||
console.log("new_user:", new_user);
|
|
||||||
|
|
||||||
return new_user;
|
return new_user;
|
||||||
});
|
});
|
||||||
console.log("ret_user:", ret_users);
|
|
||||||
|
|
||||||
res.status(HttpStatus.OK).json({ users: ret_users });
|
res.status(HttpStatus.OK).json({ users: ret_users });
|
||||||
printCaller("- out ");
|
printCaller("- out ");
|
||||||
@@ -573,8 +569,6 @@ export class ChatController {
|
|||||||
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
|
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
|
||||||
const current_room = await this.chatService.getRoomByName(room_name);
|
const current_room = await this.chatService.getRoomByName(room_name);
|
||||||
let mute = current_room.mutes.find(mute => mute.name === username);
|
let mute = current_room.mutes.find(mute => mute.name === username);
|
||||||
console.log("mute:", mute);
|
|
||||||
console.log("username:", username);
|
|
||||||
|
|
||||||
res.status(HttpStatus.OK).json({ mute: mute });
|
res.status(HttpStatus.OK).json({ mute: mute });
|
||||||
printCaller("- out ");
|
printCaller("- out ");
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import { WebSocketGateway, SubscribeMessage, WebSocketServer, MessageBody, Conne
|
|||||||
import { UsersService } from 'src/users/users.service';
|
import { UsersService } from 'src/users/users.service';
|
||||||
import { ChatService } from './chat.service';
|
import { ChatService } from './chat.service';
|
||||||
import { socketDto } from './dto/socket.dto';
|
import { socketDto } from './dto/socket.dto';
|
||||||
import { printCaller } from './dev/dev_utils';
|
import { printCaller, printCallerError, sockets, socket_server } from './dev/dev_utils';
|
||||||
|
import { Server } from 'socket.io';
|
||||||
|
|
||||||
@WebSocketGateway(5000, {
|
@WebSocketGateway(5000, {
|
||||||
path: '/chat',
|
path: '/chat',
|
||||||
@@ -20,7 +21,7 @@ implements OnGatewayConnection, OnGatewayDisconnect
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
@WebSocketServer()
|
@WebSocketServer()
|
||||||
server;
|
server: Server;
|
||||||
|
|
||||||
async handleConnection(socket: socketDto)
|
async handleConnection(socket: socketDto)
|
||||||
{
|
{
|
||||||
@@ -32,6 +33,7 @@ implements OnGatewayConnection, OnGatewayDisconnect
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
this.chatService.addSocket(socket.username, socket);
|
this.chatService.addSocket(socket.username, socket);
|
||||||
|
socket_server.server = this.server;
|
||||||
printCaller("--- socket.username:", socket.username);
|
printCaller("--- socket.username:", socket.username);
|
||||||
|
|
||||||
let not_emit: string = `${socket.username}_not_emit`;
|
let not_emit: string = `${socket.username}_not_emit`;
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import { messagesDto } from './dto/messages.dto';
|
|||||||
import { socketDto } from './dto/socket.dto';
|
import { socketDto } from './dto/socket.dto';
|
||||||
import { muteDto } from './dto/mute.dto';
|
import { muteDto } from './dto/mute.dto';
|
||||||
import * as bcrypt from 'bcrypt';
|
import * as bcrypt from 'bcrypt';
|
||||||
import { printCaller, printCallerError } from './dev/dev_utils';
|
import { printCaller, printCallerError, sockets, socket_server } from './dev/dev_utils';
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -28,28 +28,29 @@ export class ChatService {
|
|||||||
private readonly chatroomRepository: Repository<Chatroom>,
|
private readonly chatroomRepository: Repository<Chatroom>,
|
||||||
@InjectRepository(Friendship)
|
@InjectRepository(Friendship)
|
||||||
private readonly friendshipRepository: Repository<Friendship>,
|
private readonly friendshipRepository: Repository<Friendship>,
|
||||||
) {}
|
)
|
||||||
|
{
|
||||||
|
// this.sockets = new Map<string, socketDto>();
|
||||||
|
}
|
||||||
|
|
||||||
sockets = new Map<string, socketDto>();
|
// sockets = new mapSocket();
|
||||||
|
|
||||||
addSocket(key: string, socket: socketDto)
|
addSocket(key: string, socket: socketDto)
|
||||||
{
|
{
|
||||||
printCaller(`----------------- in (key: ${key}) `);
|
printCaller(`-- in (key: [${key}]) `);
|
||||||
this.sockets.set(key, socket);
|
if (key)
|
||||||
console.log("this.sockets:", this.sockets);
|
sockets.set(key, socket);
|
||||||
}
|
}
|
||||||
getSocket(key: string)
|
getSocket(key: string)
|
||||||
{
|
{
|
||||||
printCaller("----------------- in ");
|
printCaller(`-- in (key: [${key}]) `);
|
||||||
let socket = this.sockets.get(key);
|
let socket = sockets.get(key);
|
||||||
console.log("this.sockets:", this.sockets);
|
|
||||||
console.log("socket:", socket);
|
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
removeSocket(key: string)
|
removeSocket(key: string)
|
||||||
{
|
{
|
||||||
printCaller("----------------- in ");
|
printCaller(`-- in (key: [${key}]) `);
|
||||||
this.sockets.delete(key);
|
sockets.delete(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// temp for test
|
// temp for test
|
||||||
@@ -231,11 +232,6 @@ export class ChatService {
|
|||||||
printCallerError(`ERROR in chat: 'the user ${username} was not found'`);
|
printCallerError(`ERROR in chat: 'the user ${username} was not found'`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!user_db.currentRoom)
|
|
||||||
{
|
|
||||||
printCallerError(`ERROR in chat: currentRoom not found in ${user_db}`);
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
printCaller("-- out ");
|
printCaller("-- out ");
|
||||||
return user_db.currentRoom;
|
return user_db.currentRoom;
|
||||||
@@ -455,7 +451,6 @@ export class ChatService {
|
|||||||
)
|
)
|
||||||
.getOne();
|
.getOne();
|
||||||
|
|
||||||
console.log("friendship:", friendship);
|
|
||||||
if (!friendship)
|
if (!friendship)
|
||||||
return null;
|
return null;
|
||||||
return new SendableFriendship(friendship);
|
return new SendableFriendship(friendship);
|
||||||
@@ -736,7 +731,7 @@ export class ChatService {
|
|||||||
return user;
|
return user;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printCallerError(`ERROR in chat: user not found for ${username}`);
|
printCallerError(`ERROR in chat: user not found for ${username}`, user);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -810,7 +805,7 @@ export class ChatService {
|
|||||||
let room_name = await this.getCurrentRoomName(socket.username);
|
let room_name = await this.getCurrentRoomName(socket.username);
|
||||||
if (!room_name)
|
if (!room_name)
|
||||||
{
|
{
|
||||||
console.log(`couldn't find room by username: ${socket.username}`);
|
printCallerError(`ERROR in chat: couldn't find room by username: ${socket.username}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const current_room = await this.getRoomByName(room_name);
|
const current_room = await this.getRoomByName(room_name);
|
||||||
@@ -845,9 +840,7 @@ export class ChatService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("socket rooms:", socket.rooms);
|
|
||||||
let socket_name = `${socket.username}_not_emit`;
|
let socket_name = `${socket.username}_not_emit`;
|
||||||
console.log("socket_name:", socket_name);
|
|
||||||
await socket.to(socket.room).except(socket_name).emit('message', socket.username, message);
|
await socket.to(socket.room).except(socket_name).emit('message', socket.username, message);
|
||||||
await this.addMessageToRoom(room_name, socket.username, message);
|
await this.addMessageToRoom(room_name, socket.username, message);
|
||||||
|
|
||||||
@@ -890,7 +883,7 @@ export class ChatService {
|
|||||||
|
|
||||||
console.log("-- old_name:", old_name);
|
console.log("-- old_name:", old_name);
|
||||||
console.log("-- new_name:", new_name);
|
console.log("-- new_name:", new_name);
|
||||||
console.log("-- sockets:", this.sockets);
|
|
||||||
|
|
||||||
let rooms: Chatroom[] = await this.getAllRooms();
|
let rooms: Chatroom[] = await this.getAllRooms();
|
||||||
if(!rooms)
|
if(!rooms)
|
||||||
@@ -898,6 +891,7 @@ export class ChatService {
|
|||||||
printCallerError(`ERROR in chat: all rooms not found`);
|
printCallerError(`ERROR in chat: all rooms not found`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
let message = `${old_name} changes it's name for ${new_name}`;
|
||||||
await rooms.forEach((room) =>
|
await rooms.forEach((room) =>
|
||||||
{
|
{
|
||||||
let room_has_change: boolean = false;
|
let room_has_change: boolean = false;
|
||||||
@@ -943,22 +937,17 @@ export class ChatService {
|
|||||||
this.chatroomRepository.save(room);
|
this.chatroomRepository.save(room);
|
||||||
|
|
||||||
this.addMessageToRoom(room.name, "SERVER", `${old_name} changes it's name for ${new_name}`);
|
this.addMessageToRoom(room.name, "SERVER", `${old_name} changes it's name for ${new_name}`);
|
||||||
|
|
||||||
//let socket: socketDto = this.sockets.get(old_name);
|
|
||||||
//let socket = this.sockets.get(old_name);
|
|
||||||
let socket = this.getSocket(old_name);
|
|
||||||
console.log("sockets:", this.sockets);
|
|
||||||
if (socket)
|
|
||||||
{
|
|
||||||
console.log("=== found socket");
|
|
||||||
this.sockets.delete(old_name);
|
|
||||||
socket.username = new_name;
|
|
||||||
this.sockets.set(new_name, socket);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
console.log("=== socket not found");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let socket = this.getSocket(old_name);
|
||||||
|
printCaller("----------- socket", socket);
|
||||||
|
if (socket)
|
||||||
|
{
|
||||||
|
this.removeSocket(old_name);
|
||||||
|
socket.username = new_name;
|
||||||
|
this.addSocket(new_name, socket);
|
||||||
|
socket_server.server.in(socket.room).emit('message', "SERVER", message);
|
||||||
|
}
|
||||||
|
|
||||||
printCaller("-- out ");
|
printCaller("-- out ");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
import { socketDto } from 'src/chat/dto/socket.dto';
|
||||||
|
import { Server } from 'socket.io';
|
||||||
|
|
||||||
export function printCaller(...prefix)
|
export function printCaller(...prefix)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -28,3 +31,7 @@ export function printCallerError(...prefix)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const socket_server: { server: Server } = { server: null };
|
||||||
|
export const sockets = new Map<string, socketDto>();
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user