implementation of module and service socket

This commit is contained in:
AmirHossein Mahmoodi
2023-10-29 14:02:33 +03:30
parent a437a73c26
commit 67bb743a9e
11 changed files with 86 additions and 55 deletions

View File

@@ -1,7 +1,8 @@
import { Module } from "@nestjs/common";
import { NotificationModule } from "./notifications/notification.mudule";
import { NotificationModule } from "./notifications/notification.module";
import { SequelizeModule } from "@nestjs/sequelize";
import { ConfigModule } from "@nestjs/config";
import { SocketModule } from "./socket/socket.module";
@Module({
imports: [
@@ -17,6 +18,7 @@ import { ConfigModule } from "@nestjs/config";
synchronize: true,
}),
NotificationModule,
SocketModule,
],
})
export class AppModule {

View File

@@ -1,18 +1,16 @@
import { Body, Controller, Post } from "@nestjs/common";
import { NotificationDto } from "./notification.dto";
import { NotificationService } from "./notification.service";
import { SendNotificationDto } from "./validations/sendNotification.dto";
import { SocketGateway } from "../socket/socket.gateway";
@Controller("notifications")
export class NotificationController {
constructor(
private readonly notificationService: NotificationService,
private socketGateway: SocketGateway,
) {
constructor(private notificationService: NotificationService) {
}
@Post("send")
async sendNotification(@Body() notificationData: SendNotificationDto) {
return await this.socketGateway.handleSendNotification(notificationData);
async sendNotification(@Body() notificationData: NotificationDto) {
return await this.notificationService.sendNotification(notificationData);
}
}

View File

@@ -1,6 +1,6 @@
import { IsInt, IsNotEmpty, IsString } from "class-validator";
export class SendNotificationDto {
export class NotificationDto {
@IsNotEmpty()
@IsInt()
call_id: number;

View File

@@ -0,0 +1,12 @@
import { Module } from "@nestjs/common";
import { NotificationController } from "./notification.controller";
import { NotificationService } from "./notification.service";
import { SocketModule } from "../socket/socket.module";
@Module({
imports: [SocketModule],
controllers: [NotificationController],
providers: [NotificationService],
})
export class NotificationModule {
}

View File

@@ -1,15 +0,0 @@
import { Module } from "@nestjs/common";
import { NotificationController } from "./notification.controller";
import { NotificationService } from "./notification.service";
import { SocketGateway } from "../socket/socket.gateway";
import { SocketService } from "../socket/socket.service";
import { SequelizeModule } from "@nestjs/sequelize";
import { SocketModel } from "../socket/models/socket.model";
@Module({
imports: [SequelizeModule.forFeature([SocketModel])],
controllers: [NotificationController],
providers: [NotificationService, SocketGateway, SocketService],
})
export class NotificationModule {
}

View File

@@ -1,5 +1,15 @@
import { Injectable } from "@nestjs/common";
import { SocketGateway } from "../socket/socket.gateway";
import { NotificationInterface } from "./notification.interface";
@Injectable()
export class NotificationService {
constructor(
private socketGateway: SocketGateway,
) {
}
async sendNotification(notificationData: NotificationInterface) {
return await this.socketGateway.handleSendNotification(notificationData);
}
}

View File

@@ -1,6 +1,6 @@
import { OnGatewayConnection, SubscribeMessage, WebSocketGateway, WebSocketServer } from "@nestjs/websockets";
import { Server, Socket } from "socket.io";
import { NotificationInterface } from "../notifications/interfaces/notification.interface";
import { NotificationInterface } from "../notifications/notification.interface";
import { SocketService } from "./socket.service";
@WebSocketGateway()
@@ -30,32 +30,6 @@ export class SocketGateway implements OnGatewayConnection {
@SubscribeMessage("sendNotificationToClients")
handleSendNotification(notificationData: NotificationInterface) {
return new Promise(async (resolve) => {
const connectedClients = this.server.sockets.sockets;
if (connectedClients.size === 0) resolve({ status: "failed", message: "Clients not found!" });
let sent: any = false;
for (const connectedClient of connectedClients) {
if (connectedClient[1].handshake.auth.token == notificationData.telephone_id) {
sent = await new Promise((resolve) => {
connectedClient[1].timeout(2000).emit("answer", JSON.stringify(notificationData), (err: any) => {
if (err) {
resolve(false);
} else {
resolve(true);
}
});
});
}
}
if (sent) {
resolve({ status: "success", message: "Sent to clients" });
} else {
resolve({ status: "failed", message: "Not sent to clients" });
}
});
return this.socketService.sendNotificationToClients(this.server, notificationData);
}
}

View File

@@ -0,0 +1,13 @@
import { Module } from "@nestjs/common";
import { SocketGateway } from "./socket.gateway";
import { SocketService } from "./socket.service";
import { SequelizeModule } from "@nestjs/sequelize";
import { SocketModel } from "./socket.model";
@Module({
imports: [SequelizeModule.forFeature([SocketModel])],
providers: [SocketGateway, SocketService],
exports: [SocketGateway],
})
export class SocketModule {
}

View File

@@ -1,6 +1,8 @@
import { Injectable } from "@nestjs/common";
import { InjectModel } from "@nestjs/sequelize";
import { SocketModel } from "./models/socket.model";
import { SocketModel } from "./socket.model";
import { Server } from "socket.io";
import { NotificationInterface } from "../notifications/notification.interface";
@Injectable()
export class SocketService {
@@ -10,7 +12,42 @@ export class SocketService {
) {
}
ClientConnection(data) {
ClientConnection(data: any) {
this.socketModel.create(data);
}
sendNotificationToClients(server: Server, notificationData: NotificationInterface) {
return new Promise(async (resolveHandlerSend) => {
const connectedClients = server.sockets.sockets;
if (connectedClients.size === 0) {
resolveHandlerSend({ status: 404, message: "Failed to find the clients!" });
return;
}
let sent: any = false;
for (const connectedClient of connectedClients) {
if (connectedClient[1].handshake.auth.token == notificationData.telephone_id) {
sent = await new Promise((resolveSend) => {
connectedClient[1].timeout(2000).emit("answer", JSON.stringify(notificationData), (err: any) => {
if (err) {
resolveSend(false);
} else {
resolveSend(true);
}
});
});
}
}
if (!sent) {
resolveHandlerSend({ status: 404, message: "Failed to send to the clients!" });
return;
}
resolveHandlerSend({ status: 200, message: "Success in sending to the clients!" });
});
}
}