2022-09-27 00:26:10 +02:00
|
|
|
import {
|
|
|
|
HttpTransportType,
|
|
|
|
HubConnection,
|
|
|
|
HubConnectionBuilder,
|
|
|
|
IHubProtocol,
|
|
|
|
} from "@microsoft/signalr";
|
|
|
|
import { MessagePackHubProtocol } from "@microsoft/signalr-protocol-msgpack";
|
|
|
|
|
|
|
|
import { AnonymousHubService as AnonymousHubServiceAbstraction } from "../abstractions/anonymousHub.service";
|
2023-02-06 22:53:37 +01:00
|
|
|
import { AuthService } from "../auth/abstractions/auth.service";
|
2023-06-06 22:34:53 +02:00
|
|
|
import { EnvironmentService } from "../platform/abstractions/environment.service";
|
|
|
|
import { LogService } from "../platform/abstractions/log.service";
|
2022-09-27 00:26:10 +02:00
|
|
|
|
|
|
|
import {
|
|
|
|
AuthRequestPushNotification,
|
|
|
|
NotificationResponse,
|
2022-10-18 19:01:42 +02:00
|
|
|
} from "./../models/response/notification.response";
|
2022-09-27 00:26:10 +02:00
|
|
|
|
|
|
|
export class AnonymousHubService implements AnonymousHubServiceAbstraction {
|
|
|
|
private anonHubConnection: HubConnection;
|
|
|
|
private url: string;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private environmentService: EnvironmentService,
|
|
|
|
private authService: AuthService,
|
|
|
|
private logService: LogService
|
|
|
|
) {}
|
|
|
|
|
|
|
|
async createHubConnection(token: string) {
|
|
|
|
this.url = this.environmentService.getNotificationsUrl();
|
|
|
|
|
|
|
|
this.anonHubConnection = new HubConnectionBuilder()
|
2022-09-29 20:10:35 +02:00
|
|
|
.withUrl(this.url + "/anonymous-hub?Token=" + token, {
|
2022-09-27 00:26:10 +02:00
|
|
|
skipNegotiation: true,
|
|
|
|
transport: HttpTransportType.WebSockets,
|
|
|
|
})
|
|
|
|
.withHubProtocol(new MessagePackHubProtocol() as IHubProtocol)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
this.anonHubConnection.start().catch((error) => this.logService.error(error));
|
|
|
|
|
|
|
|
this.anonHubConnection.on("AuthRequestResponseRecieved", (data: any) => {
|
|
|
|
this.ProcessNotification(new NotificationResponse(data));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
stopHubConnection() {
|
|
|
|
if (this.anonHubConnection) {
|
|
|
|
this.anonHubConnection.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async ProcessNotification(notification: NotificationResponse) {
|
2023-04-26 12:16:07 +02:00
|
|
|
await this.authService.authResponsePushNotification(
|
2022-09-27 00:26:10 +02:00
|
|
|
notification.payload as AuthRequestPushNotification
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|