mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-20 04:12:23 -06:00
Simplify events structure and fix minor issues
This commit is contained in:
120
src/api/integrations/event/websocket/websocket.controller.ts
Normal file
120
src/api/integrations/event/websocket/websocket.controller.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import { EventDto } from '@api/integrations/event/event.dto';
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||
import { configService, Cors, Log, Websocket } from '@config/env.config';
|
||||
import { Logger } from '@config/logger.config';
|
||||
import { Server } from 'http';
|
||||
import { Server as SocketIO } from 'socket.io';
|
||||
|
||||
import { EmitData, EventController, EventControllerInterface } from '../event.controller';
|
||||
|
||||
export class WebsocketController extends EventController implements EventControllerInterface {
|
||||
private io: SocketIO;
|
||||
private corsConfig: Array<any>;
|
||||
private readonly logger = new Logger(WebsocketController.name);
|
||||
|
||||
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
|
||||
super(prismaRepository, waMonitor, configService.get<Websocket>('WEBSOCKET')?.ENABLED, 'websocket');
|
||||
|
||||
this.cors = configService.get<Cors>('CORS').ORIGIN;
|
||||
}
|
||||
|
||||
public init(httpServer: Server): void {
|
||||
if (!this.status) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.socket = new SocketIO(httpServer, {
|
||||
cors: {
|
||||
origin: this.cors,
|
||||
},
|
||||
});
|
||||
|
||||
this.socket.on('connection', (socket) => {
|
||||
this.logger.info('User connected');
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
this.logger.info('User disconnected');
|
||||
});
|
||||
});
|
||||
|
||||
this.logger.info('Socket.io initialized');
|
||||
}
|
||||
|
||||
private set cors(cors: Array<any>) {
|
||||
this.corsConfig = cors;
|
||||
}
|
||||
|
||||
private get cors(): string | Array<any> {
|
||||
return this.corsConfig?.includes('*') ? '*' : this.corsConfig;
|
||||
}
|
||||
|
||||
private set socket(socket: SocketIO) {
|
||||
this.io = socket;
|
||||
}
|
||||
|
||||
public get socket(): SocketIO {
|
||||
return this.io;
|
||||
}
|
||||
|
||||
public async emit({
|
||||
instanceName,
|
||||
origin,
|
||||
event,
|
||||
data,
|
||||
serverUrl,
|
||||
dateTime,
|
||||
sender,
|
||||
apiKey,
|
||||
}: EmitData): Promise<void> {
|
||||
if (!this.status) {
|
||||
return;
|
||||
}
|
||||
|
||||
const configEv = event.replace(/[.-]/gm, '_').toUpperCase();
|
||||
const logEnabled = configService.get<Log>('LOG').LEVEL.includes('WEBSOCKET');
|
||||
const message = {
|
||||
event,
|
||||
instance: instanceName,
|
||||
data,
|
||||
server_url: serverUrl,
|
||||
date_time: dateTime,
|
||||
sender,
|
||||
apikey: apiKey,
|
||||
};
|
||||
|
||||
if (configService.get<Websocket>('WEBSOCKET')?.GLOBAL_EVENTS) {
|
||||
this.socket.emit(event, message);
|
||||
|
||||
if (logEnabled) {
|
||||
this.logger.log({
|
||||
local: `${origin}.sendData-WebsocketGlobal`,
|
||||
...message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const instance = (await this.get(instanceName)) as EventDto;
|
||||
|
||||
if (!instance?.websocket.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Array.isArray(instance?.websocket.events) && instance?.websocket.events.includes(configEv)) {
|
||||
this.socket.of(`/${instanceName}`).emit(event, message);
|
||||
|
||||
if (logEnabled) {
|
||||
this.logger.log({
|
||||
local: `${origin}.sendData-Websocket`,
|
||||
...message,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (logEnabled) {
|
||||
this.logger.log(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user