socket reconnecte automatically to last room and blocked users

This commit is contained in:
simplonco
2023-01-16 14:10:12 +01:00
parent 20ea0bcef5
commit 9d5aa7f3a1
3 changed files with 17 additions and 29 deletions

View File

@@ -2,6 +2,7 @@ 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';
@WebSocketGateway(5000, {
path: '/chat',
@@ -22,12 +23,24 @@ implements OnGatewayConnection, OnGatewayDisconnect
server;
async handleConnection(socket: socketDto) {
console.log('- socket connected :', socket.id, socket.handshake.query.username);
printCaller('- socket connected :', socket.id, socket.handshake.query.username);
socket.username = socket.handshake.query.username.toString();
this.sockets.set(socket.username, socket);
printCaller("socket.username:", socket.username);
let not_emit: string = `${socket.username}_not_emit`;
socket.join(not_emit);
let blocked = await this.chatService.getListBlockUser(socket.username);
blocked.forEach(user =>
{
not_emit = `${user}_not_emit`;
socket.join(not_emit);
});
let current_room = await this.chatService.getCurrentRoomName(socket.username);
socket.join(current_room);
}
async handleDisconnect(socket: socketDto) {
this.sockets.delete(socket.username);

View File

@@ -377,37 +377,11 @@ export class ChatService {
let user = await this.getUserByName(username);
let friends_users = await this.friendshipService.findAllBlockedFriends(user.id);
let users = friends_users.map(user => user.receiverUsername);
console.log(users);
printCaller("-- out ");
return users;
}
/*
// find list blocked
findAllBlockedFriends
extract the receiver_username (i am the sender)
// bock a user
findfriendshipbyname
return friendship
blockfriendship
return nothing
createfriendship
return friendship (it worked)
return http exception (cannot create friendship)
// unblock
findfriendshipbyname
return friendship
removefriendship
return nothing
*/
/* ADDERS *************************************************
*/

View File

@@ -1,4 +1,5 @@
export function printCaller(prefix: string = "") {
export function printCaller(...prefix)
{
try
{
throw new Error();
@@ -8,6 +9,6 @@ export function printCaller(prefix: string = "") {
Error.captureStackTrace(e);
let stack = e.stack.split('\n');
let caller = stack[2].trim();
console.log(prefix + ' ' + caller);
console.log(...prefix, caller);
}
}