This commit is contained in:
Amirhossein Mahmoodi
2024-07-29 16:15:53 +03:30
parent d09567d9d5
commit 5af4d291b2
5 changed files with 49 additions and 55 deletions

View File

@@ -15,13 +15,13 @@ export class SocketGateway implements OnGatewayConnection {
constructor(private socketService: SocketService) {}
async handleConnection(client: Socket) {
await this.socketService.ClientConnection({
await this.socketService.clientConnection({
client_id: client.handshake.auth.token,
socket_id: client.id,
event: 'connected',
});
client.on('disconnect', async (reason) => {
await this.socketService.ClientConnection({
await this.socketService.clientConnection({
client_id: client.handshake.auth.token,
socket_id: client.id,
event: 'disconnected',
@@ -31,28 +31,31 @@ export class SocketGateway implements OnGatewayConnection {
}
@SubscribeMessage('sendMessageToClients')
handleSendMessage(sendMessageDto: SendMessageDto) {
const clients = this.getConnectedClients(sendMessageDto.auth);
async handleSendMessage(sendMessageDto: SendMessageDto) {
const clients = this.getConnectedClients(sendMessageDto.auths);
if (clients.length === 0) {
return {
status: 0,
message: 'Failed to find the clients!',
};
}
return this.socketService.sendMessageToClients(
const result = await this.socketService.sendMessageToClients(
clients,
sendMessageDto.data,
sendMessageDto.event,
);
return result;
}
getConnectedClients(auth: string[]): Socket[] {
getConnectedClients(auths: string[]): Socket[] {
const connectedClients = this.server.sockets.sockets;
const clients: Socket[] = [];
const clients = [];
for (const connectedClient of connectedClients) {
if (auth.includes(connectedClient[1].handshake.auth.token)) {
clients.push(connectedClient[1]);
for (const client of connectedClients.values()) {
if (auths.includes(client.handshake.auth.token)) {
clients.push(client);
}
}