2024-07-11 10:28:21 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import { BehaviorSubject } from 'rxjs';
|
|
|
|
|
import { Platform } from '@ionic/angular';
|
|
|
|
|
import { SignalRConnection } from './signalR';
|
|
|
|
|
import { Plugins } from '@capacitor/core';
|
2024-07-18 16:19:30 +01:00
|
|
|
import { z } from 'zod';
|
|
|
|
|
import { UserTypingDTO } from '../../data/dto/typing/typingInputDTO';
|
|
|
|
|
import { MessageOutPutDataDTO } from '../../data/dto/message/messageOutputDTO';
|
2024-07-11 10:28:21 +01:00
|
|
|
|
|
|
|
|
const { App } = Plugins;
|
|
|
|
|
|
|
|
|
|
|
2024-07-18 16:19:30 +01:00
|
|
|
|
2024-07-11 10:28:21 +01:00
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class SignalRService {
|
|
|
|
|
private connection: SignalRConnection;
|
2024-07-18 16:19:30 +01:00
|
|
|
private messageSubject: BehaviorSubject<MessageOutPutDataDTO> = new BehaviorSubject<MessageOutPutDataDTO>(null);
|
|
|
|
|
private typingSubject: BehaviorSubject<UserTypingDTO> = new BehaviorSubject<UserTypingDTO>(null);
|
2024-07-11 10:28:21 +01:00
|
|
|
private connectingSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);
|
|
|
|
|
|
|
|
|
|
constructor(
|
2024-07-17 16:39:18 +01:00
|
|
|
private platform: Platform) {
|
2024-07-11 10:28:21 +01:00
|
|
|
// 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() {
|
|
|
|
|
|
2024-07-31 10:00:44 +01:00
|
|
|
const connection = new SignalRConnection({url:'https://41e3-41-63-166-54.ngrok-free.app/api/v2/chathub'})
|
2024-07-11 10:28:21 +01:00
|
|
|
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)
|
|
|
|
|
})
|
2024-07-17 16:39:18 +01:00
|
|
|
this.connection.getTyping().subscribe((data) => {
|
|
|
|
|
this.typingSubject.next(data)
|
|
|
|
|
})
|
2024-07-11 10:28:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getMessage() {
|
|
|
|
|
return this.messageSubject.asObservable()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 16:39:18 +01:00
|
|
|
getTyping() {
|
2024-07-18 16:19:30 +01:00
|
|
|
return this.typingSubject.asObservable().pipe()
|
2024-07-17 16:39:18 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-11 10:28:21 +01:00
|
|
|
async sendMessage(data: Object) {
|
|
|
|
|
return await this.connection.sendMessage(data as any)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newConnection() {
|
|
|
|
|
this.establishConnection()
|
|
|
|
|
}
|
2024-07-17 16:39:18 +01:00
|
|
|
|
2024-07-25 08:51:04 +01:00
|
|
|
async sendTyping({roomId, UserName, userId}) {
|
|
|
|
|
return await this.connection.typing({ roomId, UserName, userId})
|
2024-07-18 16:19:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendReadAt({ roomId, memberId, chatMessageId}) {
|
|
|
|
|
return await this.connection.sendReadAt({ roomId, memberId, chatMessageId})
|
2024-07-17 16:39:18 +01:00
|
|
|
}
|
2024-07-11 10:28:21 +01:00
|
|
|
}
|