mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 13:02:56 +00:00
99 lines
2.6 KiB
TypeScript
99 lines
2.6 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { BehaviorSubject } from 'rxjs';
|
|
import { Platform } from '@ionic/angular';
|
|
import { SignalRConnection } from './signalR';
|
|
import { Plugins } from '@capacitor/core';
|
|
import { z } from 'zod';
|
|
import { UserTypingDTO } from '../../data/dto/typing/typingInputDTO';
|
|
import { MessageOutPutDataDTO } from '../../data/dto/message/messageOutputDTO';
|
|
|
|
const { App } = Plugins;
|
|
|
|
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class SignalRService {
|
|
private connection: SignalRConnection;
|
|
private messageSubject: BehaviorSubject<MessageOutPutDataDTO> = new BehaviorSubject<MessageOutPutDataDTO>(null);
|
|
private typingSubject: BehaviorSubject<UserTypingDTO> = new BehaviorSubject<UserTypingDTO>(null);
|
|
private connectingSubject: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null);
|
|
|
|
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://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)
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
|
|
getMessage() {
|
|
return this.messageSubject.asObservable()
|
|
|
|
}
|
|
|
|
getTyping() {
|
|
return this.typingSubject.asObservable().pipe()
|
|
}
|
|
|
|
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})
|
|
}
|
|
}
|