socket and server are now in separate files
This commit is contained in:
@@ -263,3 +263,8 @@
|
||||
- user join empty protected room
|
||||
- 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 { socketDto } from './dto/socket.dto';
|
||||
import { roomDto } from './dto/room.dto';
|
||||
import { printCaller } from './dev/dev_utils';
|
||||
import { setCurrentRoomDto } from './dto/setCurrentRoom.dto';
|
||||
import { muteDto } from './dto/mute.dto';
|
||||
import { printCaller, printCallerError } from './dev/dev_utils';
|
||||
|
||||
@Controller('chat')
|
||||
export class ChatController {
|
||||
@@ -52,7 +52,6 @@ export class ChatController {
|
||||
|
||||
let fields = ["name", "type", "users", "protection", "allowed_users"];
|
||||
const rooms = await this.chatService.getMyRooms(req.user.username, fields);
|
||||
console.log("rooms:", rooms);
|
||||
const blocked = await this.chatService.getListBlockUser(req.user.username);
|
||||
|
||||
let filtered_rooms = rooms.filter(room =>
|
||||
@@ -466,7 +465,6 @@ export class ChatController {
|
||||
|
||||
const messages = await this.chatService.getMessagesFromCurrentRoom(req.user.username);
|
||||
|
||||
console.log("messages:", messages);
|
||||
const blocked = await this.chatService.getListBlockUser(req.user.username);
|
||||
let filtered_messages = messages.filter(message =>
|
||||
{
|
||||
@@ -508,11 +506,9 @@ export class ChatController {
|
||||
new_user.isadmin = true;
|
||||
if (blocked.includes(username))
|
||||
new_user.isblocked = true;
|
||||
console.log("new_user:", new_user);
|
||||
|
||||
return new_user;
|
||||
});
|
||||
console.log("ret_user:", ret_users);
|
||||
|
||||
res.status(HttpStatus.OK).json({ users: ret_users });
|
||||
printCaller("- out ");
|
||||
@@ -573,8 +569,6 @@ export class ChatController {
|
||||
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
|
||||
const current_room = await this.chatService.getRoomByName(room_name);
|
||||
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 });
|
||||
printCaller("- out ");
|
||||
|
||||
@@ -4,7 +4,8 @@ import { WebSocketGateway, SubscribeMessage, WebSocketServer, MessageBody, Conne
|
||||
import { UsersService } from 'src/users/users.service';
|
||||
import { ChatService } from './chat.service';
|
||||
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, {
|
||||
path: '/chat',
|
||||
@@ -20,7 +21,7 @@ implements OnGatewayConnection, OnGatewayDisconnect
|
||||
) {}
|
||||
|
||||
@WebSocketServer()
|
||||
server;
|
||||
server: Server;
|
||||
|
||||
async handleConnection(socket: socketDto)
|
||||
{
|
||||
@@ -32,6 +33,7 @@ implements OnGatewayConnection, OnGatewayDisconnect
|
||||
return;
|
||||
|
||||
this.chatService.addSocket(socket.username, socket);
|
||||
socket_server.server = this.server;
|
||||
printCaller("--- socket.username:", socket.username);
|
||||
|
||||
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 { muteDto } from './dto/mute.dto';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { printCaller, printCallerError } from './dev/dev_utils';
|
||||
import { printCaller, printCallerError, sockets, socket_server } from './dev/dev_utils';
|
||||
|
||||
|
||||
@Injectable()
|
||||
@@ -28,28 +28,29 @@ export class ChatService {
|
||||
private readonly chatroomRepository: Repository<Chatroom>,
|
||||
@InjectRepository(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)
|
||||
{
|
||||
printCaller(`----------------- in (key: ${key}) `);
|
||||
this.sockets.set(key, socket);
|
||||
console.log("this.sockets:", this.sockets);
|
||||
printCaller(`-- in (key: [${key}]) `);
|
||||
if (key)
|
||||
sockets.set(key, socket);
|
||||
}
|
||||
getSocket(key: string)
|
||||
{
|
||||
printCaller("----------------- in ");
|
||||
let socket = this.sockets.get(key);
|
||||
console.log("this.sockets:", this.sockets);
|
||||
console.log("socket:", socket);
|
||||
printCaller(`-- in (key: [${key}]) `);
|
||||
let socket = sockets.get(key);
|
||||
return socket;
|
||||
}
|
||||
removeSocket(key: string)
|
||||
{
|
||||
printCaller("----------------- in ");
|
||||
this.sockets.delete(key);
|
||||
printCaller(`-- in (key: [${key}]) `);
|
||||
sockets.delete(key);
|
||||
}
|
||||
|
||||
// temp for test
|
||||
@@ -231,11 +232,6 @@ export class ChatService {
|
||||
printCallerError(`ERROR in chat: 'the user ${username} was not found'`);
|
||||
return;
|
||||
}
|
||||
if (!user_db.currentRoom)
|
||||
{
|
||||
printCallerError(`ERROR in chat: currentRoom not found in ${user_db}`);
|
||||
return "";
|
||||
}
|
||||
|
||||
printCaller("-- out ");
|
||||
return user_db.currentRoom;
|
||||
@@ -455,7 +451,6 @@ export class ChatService {
|
||||
)
|
||||
.getOne();
|
||||
|
||||
console.log("friendship:", friendship);
|
||||
if (!friendship)
|
||||
return null;
|
||||
return new SendableFriendship(friendship);
|
||||
@@ -736,7 +731,7 @@ export class ChatService {
|
||||
return user;
|
||||
else
|
||||
{
|
||||
printCallerError(`ERROR in chat: user not found for ${username}`);
|
||||
printCallerError(`ERROR in chat: user not found for ${username}`, user);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -810,7 +805,7 @@ export class ChatService {
|
||||
let room_name = await this.getCurrentRoomName(socket.username);
|
||||
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;
|
||||
}
|
||||
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`;
|
||||
console.log("socket_name:", socket_name);
|
||||
await socket.to(socket.room).except(socket_name).emit('message', 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("-- new_name:", new_name);
|
||||
console.log("-- sockets:", this.sockets);
|
||||
|
||||
|
||||
let rooms: Chatroom[] = await this.getAllRooms();
|
||||
if(!rooms)
|
||||
@@ -898,6 +891,7 @@ export class ChatService {
|
||||
printCallerError(`ERROR in chat: all rooms not found`);
|
||||
return null;
|
||||
}
|
||||
let message = `${old_name} changes it's name for ${new_name}`;
|
||||
await rooms.forEach((room) =>
|
||||
{
|
||||
let room_has_change: boolean = false;
|
||||
@@ -943,22 +937,17 @@ export class ChatService {
|
||||
this.chatroomRepository.save(room);
|
||||
|
||||
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 ");
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import { socketDto } from 'src/chat/dto/socket.dto';
|
||||
import { Server } from 'socket.io';
|
||||
|
||||
export function printCaller(...prefix)
|
||||
{
|
||||
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