update
This commit is contained in:
@@ -4,10 +4,12 @@ import { NotificationService } from './notification.service';
|
||||
|
||||
@Controller('notifications')
|
||||
export class NotificationController {
|
||||
constructor(private notificationService: NotificationService) {}
|
||||
constructor(private readonly notificationService: NotificationService) {}
|
||||
|
||||
@Post('send')
|
||||
async sendNotification(@Body() notificationData: NotificationDto) {
|
||||
async sendNotification(
|
||||
@Body() notificationData: NotificationDto,
|
||||
): Promise<{ status: number; message: string }> {
|
||||
return await this.notificationService.sendNotification(notificationData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@ import { NotificationDto } from './notification.dto';
|
||||
|
||||
@Injectable()
|
||||
export class NotificationService {
|
||||
constructor(private socketGateway: SocketGateway) {}
|
||||
constructor(private readonly socketGateway: SocketGateway) {}
|
||||
|
||||
async sendNotification(notificationData: NotificationDto) {
|
||||
const auth: string[] = [];
|
||||
auth.push(notificationData.telephone_id);
|
||||
async sendNotification(
|
||||
notificationData: NotificationDto,
|
||||
): Promise<{ status: number; message: string }> {
|
||||
return await this.socketGateway.handleSendMessage({
|
||||
auth,
|
||||
auths: [notificationData.telephone_id],
|
||||
data: notificationData,
|
||||
event: 'answer',
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export class SendMessageDto {
|
||||
auth: string[];
|
||||
auths: string[];
|
||||
data: any;
|
||||
event: string;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,49 +12,38 @@ export class SocketService {
|
||||
private socketConnectionRepository: Repository<SocketConnection>,
|
||||
) {}
|
||||
|
||||
async ClientConnection(data: ClientConnectionDto) {
|
||||
async clientConnection(data: ClientConnectionDto) {
|
||||
const client = this.socketConnectionRepository.create(data);
|
||||
await this.socketConnectionRepository.save(client);
|
||||
}
|
||||
|
||||
sendMessageToClients(clients: Socket[], data: any, event: string) {
|
||||
return new Promise(async (resolveHandlerSend) => {
|
||||
let sent: boolean = false;
|
||||
|
||||
try {
|
||||
for (const client of clients) {
|
||||
sent = await new Promise((resolveSend) => {
|
||||
client
|
||||
.timeout(2000)
|
||||
.emit(event, JSON.stringify(data), (err: any) => {
|
||||
if (err) {
|
||||
resolveSend(false);
|
||||
} else {
|
||||
resolveSend(true);
|
||||
}
|
||||
});
|
||||
async sendMessageToClients(
|
||||
clients: Socket[],
|
||||
data: any,
|
||||
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) => {
|
||||
resolve(!err);
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
resolveHandlerSend({
|
||||
status: 0,
|
||||
message: 'Failed to send to the clients!',
|
||||
});
|
||||
return;
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
if (!sent) {
|
||||
resolveHandlerSend({
|
||||
status: 0,
|
||||
message: 'Failed to send to the clients!',
|
||||
});
|
||||
return;
|
||||
}
|
||||
const results = await Promise.all(promises);
|
||||
const allSent = results.every((result) => result);
|
||||
|
||||
resolveHandlerSend({
|
||||
status: 1,
|
||||
message: 'Success in sending to the clients!',
|
||||
});
|
||||
});
|
||||
if (!allSent) {
|
||||
return {
|
||||
status: 0,
|
||||
message: 'Failed to send to all clients!',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
status: 1,
|
||||
message: 'Success in sending to all clients!',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user