Files
doneit-web/src/app/module/chat/infra/socket/signal-r.service.ts
T

156 lines
4.3 KiB
TypeScript
Raw Normal View History

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 { UserTypingDTO } from '../../data/dto/typing/typingInputDTO';
import { MessageOutPutDataDTO } from '../../data/dto/message/messageOutputDTO';
2024-07-31 17:23:44 +01:00
import { MessageDeleteInputDTO } from '../../data/dto/message/messageDeleteInputDTO';
2024-08-02 11:34:57 +01:00
import { object, z } from 'zod';
2024-07-11 10:28:21 +01:00
const { App } = Plugins;
2024-08-02 11:34:57 +01:00
const SignalRInputSchema = z.object({
method: z.string(),
data: z.object({
requestId: z.string()
})
})
2024-07-11 10:28:21 +01:00
2024-08-02 11:34:57 +01:00
export type ISignalRInput = z.infer<typeof SignalRInputSchema>
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);
2024-07-31 17:23:44 +01:00
private messageDelete: BehaviorSubject<MessageOutPutDataDTO> = new BehaviorSubject<MessageOutPutDataDTO>(null);
private messageUpdateSubject: BehaviorSubject<MessageOutPutDataDTO> = new BehaviorSubject<MessageOutPutDataDTO>(null);
2024-08-02 11:34:57 +01:00
private sendDataSubject: BehaviorSubject<Object> = new BehaviorSubject<Object>(false);
2024-07-11 10:28:21 +01:00
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 17:23:44 +01:00
// 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'})
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-31 17:23:44 +01:00
this.connection.getMessageDelete().subscribe((data) => {
this.messageDelete.next(data)
})
this.connection.getMessageUpdateSubject().subscribe((data) => {
this.messageUpdateSubject.next(data)
})
2024-08-02 11:34:57 +01:00
this.connection.getMessageUpdateSubject().subscribe((data) => {
this.messageUpdateSubject.next(data)
})
this.connection.getData().subscribe((data) => {
this.messageUpdateSubject.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-31 17:23:44 +01:00
getMessageDelete() {
return this.messageDelete.asObservable()
}
getMessageUpdate() {
return this.messageUpdateSubject.asObservable()
}
2024-08-02 11:34:57 +01:00
sendData(input: ISignalRInput) {
return this.connection.sendData(input)
}
getData() {
return this.getData()
}
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-31 17:23:44 +01:00
async sendMessageDelete(data: MessageDeleteInputDTO) {
return await this.connection.deleteMessage(data)
}
2024-08-02 11:34:57 +01:00
async sendReactToMessage(data) {
return await this.connection.sendReactMessage(data)
}
2024-07-11 10:28:21 +01:00
}