import { Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; import { Platform } from '@ionic/angular'; import { SignalRConnection } from './signalR'; import { Plugins } from '@capacitor/core'; import { UserTypingDTO } from '../../data/dto/typing/typingInputDTO'; import { MessageOutPutDataDTO } from '../../data/dto/message/messageOutputDTO'; import { MessageDeleteInputDTO } from '../../data/dto/message/messageDeleteInputDTO'; import { object, z } from 'zod'; const { App } = Plugins; const SignalRInputSchema = z.object({ method: z.string(), data: z.object({ requestId: z.string() }) }) export type ISignalRInput = z.infer @Injectable({ providedIn: 'root' }) export class SignalRService { private connection: SignalRConnection; private messageSubject: BehaviorSubject = new BehaviorSubject(null); private typingSubject: BehaviorSubject = new BehaviorSubject(null); private connectingSubject: BehaviorSubject = new BehaviorSubject(null); private messageDelete: BehaviorSubject = new BehaviorSubject(null); private messageUpdateSubject: BehaviorSubject = new BehaviorSubject(null); private sendDataSubject: BehaviorSubject = new BehaviorSubject(false); constructor( private platform: Platform) { // this.startConnection(); // this.addMessageListener(); try { if (!this.platform.is('desktop')) { App.addListener('appStateChange', ({ isActive }) => { if (isActive) { // The app is in the foreground. console.log('App is in the foreground'); this.newConnection() } else { // The app is in the background. console.log('App is in the background'); // You can perform actions specific to the background state here. } }); } } catch(error) {} this.establishConnection() } private async establishConnection() { // const connection = new SignalRConnection({url:'https://41e3-41-63-166-54.ngrok-free.app/api/v2/chathub'}) const connection = new SignalRConnection({url:'https://gdapi-dev.dyndns.info/stage/api/v2/chathub'}) const attempConnection = await connection.establishConnection() if(attempConnection.isOk()) { this.connection?.closeConnection() this.connection = connection this.connection.getSendLater().subscribe(data => { }) this.connection.getMessages().subscribe((data) => { this.messageSubject.next(data) }) this.connection.getTyping().subscribe((data) => { this.typingSubject.next(data) }) this.connection.getMessageDelete().subscribe((data) => { this.messageDelete.next(data) }) this.connection.getMessageUpdateSubject().subscribe((data) => { this.messageUpdateSubject.next(data) }) this.connection.getMessageUpdateSubject().subscribe((data) => { this.messageUpdateSubject.next(data) }) this.connection.getData().subscribe((data) => { this.messageUpdateSubject.next(data) }) } } getMessage() { return this.messageSubject.asObservable() } getTyping() { return this.typingSubject.asObservable().pipe() } getMessageDelete() { return this.messageDelete.asObservable() } getMessageUpdate() { return this.messageUpdateSubject.asObservable() } sendData(input: ISignalRInput) { return this.connection.sendData(input) } getData() { return this.getData() } async sendMessage(data: Object) { return await this.connection.sendMessage(data as any) } newConnection() { this.establishConnection() } async sendTyping({roomId, UserName, userId}) { return await this.connection.typing({ roomId, UserName, userId}) } async sendReadAt({ roomId, memberId, chatMessageId}) { return await this.connection.sendReadAt({ roomId, memberId, chatMessageId}) } async sendMessageDelete(data: MessageDeleteInputDTO) { return await this.connection.deleteMessage(data) } async sendReactToMessage(data) { return await this.connection.sendReactMessage(data) } }