update
This commit is contained in:
@@ -4,10 +4,12 @@ import { NotificationService } from './notification.service';
|
|||||||
|
|
||||||
@Controller('notifications')
|
@Controller('notifications')
|
||||||
export class NotificationController {
|
export class NotificationController {
|
||||||
constructor(private notificationService: NotificationService) {}
|
constructor(private readonly notificationService: NotificationService) {}
|
||||||
|
|
||||||
@Post('send')
|
@Post('send')
|
||||||
async sendNotification(@Body() notificationData: NotificationDto) {
|
async sendNotification(
|
||||||
|
@Body() notificationData: NotificationDto,
|
||||||
|
): Promise<{ status: number; message: string }> {
|
||||||
return await this.notificationService.sendNotification(notificationData);
|
return await this.notificationService.sendNotification(notificationData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ import { NotificationDto } from './notification.dto';
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class NotificationService {
|
export class NotificationService {
|
||||||
constructor(private socketGateway: SocketGateway) {}
|
constructor(private readonly socketGateway: SocketGateway) {}
|
||||||
|
|
||||||
async sendNotification(notificationData: NotificationDto) {
|
async sendNotification(
|
||||||
const auth: string[] = [];
|
notificationData: NotificationDto,
|
||||||
auth.push(notificationData.telephone_id);
|
): Promise<{ status: number; message: string }> {
|
||||||
return await this.socketGateway.handleSendMessage({
|
return await this.socketGateway.handleSendMessage({
|
||||||
auth,
|
auths: [notificationData.telephone_id],
|
||||||
data: notificationData,
|
data: notificationData,
|
||||||
event: 'answer',
|
event: 'answer',
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
export class SendMessageDto {
|
export class SendMessageDto {
|
||||||
auth: string[];
|
auths: string[];
|
||||||
data: any;
|
data: any;
|
||||||
event: string;
|
event: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,13 +15,13 @@ export class SocketGateway implements OnGatewayConnection {
|
|||||||
constructor(private socketService: SocketService) {}
|
constructor(private socketService: SocketService) {}
|
||||||
|
|
||||||
async handleConnection(client: Socket) {
|
async handleConnection(client: Socket) {
|
||||||
await this.socketService.ClientConnection({
|
await this.socketService.clientConnection({
|
||||||
client_id: client.handshake.auth.token,
|
client_id: client.handshake.auth.token,
|
||||||
socket_id: client.id,
|
socket_id: client.id,
|
||||||
event: 'connected',
|
event: 'connected',
|
||||||
});
|
});
|
||||||
client.on('disconnect', async (reason) => {
|
client.on('disconnect', async (reason) => {
|
||||||
await this.socketService.ClientConnection({
|
await this.socketService.clientConnection({
|
||||||
client_id: client.handshake.auth.token,
|
client_id: client.handshake.auth.token,
|
||||||
socket_id: client.id,
|
socket_id: client.id,
|
||||||
event: 'disconnected',
|
event: 'disconnected',
|
||||||
@@ -31,28 +31,31 @@ export class SocketGateway implements OnGatewayConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeMessage('sendMessageToClients')
|
@SubscribeMessage('sendMessageToClients')
|
||||||
handleSendMessage(sendMessageDto: SendMessageDto) {
|
async handleSendMessage(sendMessageDto: SendMessageDto) {
|
||||||
const clients = this.getConnectedClients(sendMessageDto.auth);
|
const clients = this.getConnectedClients(sendMessageDto.auths);
|
||||||
if (clients.length === 0) {
|
if (clients.length === 0) {
|
||||||
return {
|
return {
|
||||||
status: 0,
|
status: 0,
|
||||||
message: 'Failed to find the clients!',
|
message: 'Failed to find the clients!',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return this.socketService.sendMessageToClients(
|
|
||||||
|
const result = await this.socketService.sendMessageToClients(
|
||||||
clients,
|
clients,
|
||||||
sendMessageDto.data,
|
sendMessageDto.data,
|
||||||
sendMessageDto.event,
|
sendMessageDto.event,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
getConnectedClients(auth: string[]): Socket[] {
|
getConnectedClients(auths: string[]): Socket[] {
|
||||||
const connectedClients = this.server.sockets.sockets;
|
const connectedClients = this.server.sockets.sockets;
|
||||||
|
const clients: Socket[] = [];
|
||||||
|
|
||||||
const clients = [];
|
for (const client of connectedClients.values()) {
|
||||||
for (const connectedClient of connectedClients) {
|
if (auths.includes(client.handshake.auth.token)) {
|
||||||
if (auth.includes(connectedClient[1].handshake.auth.token)) {
|
clients.push(client);
|
||||||
clients.push(connectedClient[1]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,49 +12,38 @@ export class SocketService {
|
|||||||
private socketConnectionRepository: Repository<SocketConnection>,
|
private socketConnectionRepository: Repository<SocketConnection>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async ClientConnection(data: ClientConnectionDto) {
|
async clientConnection(data: ClientConnectionDto) {
|
||||||
const client = this.socketConnectionRepository.create(data);
|
const client = this.socketConnectionRepository.create(data);
|
||||||
await this.socketConnectionRepository.save(client);
|
await this.socketConnectionRepository.save(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessageToClients(clients: Socket[], data: any, event: string) {
|
async sendMessageToClients(
|
||||||
return new Promise(async (resolveHandlerSend) => {
|
clients: Socket[],
|
||||||
let sent: boolean = false;
|
data: any,
|
||||||
|
event: string,
|
||||||
try {
|
): Promise<{ status: number; message: string }> {
|
||||||
for (const client of clients) {
|
const promises = clients.map(
|
||||||
sent = await new Promise((resolveSend) => {
|
(client) =>
|
||||||
client
|
new Promise<boolean>((resolve) => {
|
||||||
.timeout(2000)
|
client.timeout(2000).emit(event, JSON.stringify(data), (err: any) => {
|
||||||
.emit(event, JSON.stringify(data), (err: any) => {
|
resolve(!err);
|
||||||
if (err) {
|
|
||||||
resolveSend(false);
|
|
||||||
} else {
|
|
||||||
resolveSend(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}),
|
||||||
} catch (error) {
|
);
|
||||||
resolveHandlerSend({
|
|
||||||
status: 0,
|
|
||||||
message: 'Failed to send to the clients!',
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!sent) {
|
const results = await Promise.all(promises);
|
||||||
resolveHandlerSend({
|
const allSent = results.every((result) => result);
|
||||||
status: 0,
|
|
||||||
message: 'Failed to send to the clients!',
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
resolveHandlerSend({
|
if (!allSent) {
|
||||||
status: 1,
|
return {
|
||||||
message: 'Success in sending to the clients!',
|
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