add online/offline api
This commit is contained in:
@@ -7,7 +7,6 @@ import {
|
||||
import { Server, Socket } from 'socket.io';
|
||||
import { SendMessageDto } from './dto/sendMessage.dto';
|
||||
import { SocketService } from './socket.service';
|
||||
import { Logger } from '@nestjs/common';
|
||||
|
||||
@WebSocketGateway({
|
||||
cors: {
|
||||
@@ -16,15 +15,12 @@ import { Logger } from '@nestjs/common';
|
||||
path: '/notification/socket.io',
|
||||
})
|
||||
export class SocketGateway implements OnGatewayConnection {
|
||||
private readonly logger = new Logger(SocketGateway.name);
|
||||
|
||||
@WebSocketServer()
|
||||
server: Server;
|
||||
|
||||
constructor(private socketService: SocketService) {}
|
||||
|
||||
async handleConnection(client: Socket) {
|
||||
this.logger.log(`Client connected: ${client.id}`);
|
||||
await this.socketService.clientConnection({
|
||||
client_id: client.handshake.auth.token,
|
||||
socket_id: client.id,
|
||||
|
||||
@@ -3,9 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SocketConnection } from './socket.entity';
|
||||
import { SocketGateway } from './socket.gateway';
|
||||
import { SocketService } from './socket.service';
|
||||
import { HttpModule } from '@nestjs/axios';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([SocketConnection])],
|
||||
imports: [TypeOrmModule.forFeature([SocketConnection]), HttpModule],
|
||||
providers: [SocketGateway, SocketService],
|
||||
exports: [SocketGateway],
|
||||
})
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import { Socket } from 'socket.io';
|
||||
import { Repository } from 'typeorm';
|
||||
import { ClientConnectionDto } from './dto/clientConnection.dto';
|
||||
@@ -7,13 +9,38 @@ import { SocketConnection } from './socket.entity';
|
||||
|
||||
@Injectable()
|
||||
export class SocketService {
|
||||
private readonly logger = new Logger(SocketService.name);
|
||||
|
||||
constructor(
|
||||
@InjectRepository(SocketConnection)
|
||||
private socketConnectionRepository: Repository<SocketConnection>,
|
||||
private readonly httpService: HttpService,
|
||||
) {}
|
||||
|
||||
async clientConnection(data: ClientConnectionDto) {
|
||||
const client = this.socketConnectionRepository.create(data);
|
||||
|
||||
const eventEndpoints = {
|
||||
connected: '/api/operator/online',
|
||||
disconnected: '/api/operator/offline',
|
||||
};
|
||||
|
||||
const endpoint = eventEndpoints[data.event];
|
||||
|
||||
if (endpoint) {
|
||||
const url = `${process.env.SERVICE_URL}${endpoint}?telephone_id=${client.client_id}`;
|
||||
try {
|
||||
await firstValueFrom(this.httpService.get(url));
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`❌ Failed to notify ${data.event} for client ${client.client_id}:`,
|
||||
error.message || error,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
this.logger.warn(`⚠️ Unknown event type: ${data.event}`);
|
||||
}
|
||||
|
||||
await this.socketConnectionRepository.save(client);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user