This commit is contained in:
Amirhossein Mahmoodi
2024-07-29 16:27:56 +03:30
parent 5af4d291b2
commit afb25635df
4 changed files with 10 additions and 10 deletions

View File

@@ -10,8 +10,8 @@ export class NotificationService {
notificationData: NotificationDto,
): Promise<{ status: number; message: string }> {
return await this.socketGateway.handleSendMessage({
auths: [notificationData.telephone_id],
data: notificationData,
clientsId: [notificationData.telephone_id],
message: JSON.stringify(notificationData),
event: 'answer',
});
}

View File

@@ -1,5 +1,5 @@
export class SendMessageDto {
auths: string[];
data: any;
clientsId: string[];
message: string;
event: string;
}

View File

@@ -32,7 +32,7 @@ export class SocketGateway implements OnGatewayConnection {
@SubscribeMessage('sendMessageToClients')
async handleSendMessage(sendMessageDto: SendMessageDto) {
const clients = this.getConnectedClients(sendMessageDto.auths);
const clients = this.getConnectedClients(sendMessageDto.clientsId);
if (clients.length === 0) {
return {
status: 0,
@@ -42,19 +42,19 @@ export class SocketGateway implements OnGatewayConnection {
const result = await this.socketService.sendMessageToClients(
clients,
sendMessageDto.data,
sendMessageDto.message,
sendMessageDto.event,
);
return result;
}
getConnectedClients(auths: string[]): Socket[] {
getConnectedClients(clientsId: string[]): Socket[] {
const connectedClients = this.server.sockets.sockets;
const clients: Socket[] = [];
for (const client of connectedClients.values()) {
if (auths.includes(client.handshake.auth.token)) {
if (clientsId.includes(client.handshake.auth.token)) {
clients.push(client);
}
}

View File

@@ -19,13 +19,13 @@ export class SocketService {
async sendMessageToClients(
clients: Socket[],
data: any,
message: string,
event: string,
): Promise<{ status: number; message: string }> {
const promises = clients.map(
(client) =>
new Promise<boolean>((resolve) => {
client.timeout(2000).emit(event, JSON.stringify(data), (err: any) => {
client.timeout(2000).emit(event, message, (err: any) => {
resolve(!err);
});
}),