63 lines
1.4 KiB
TypeScript
63 lines
1.4 KiB
TypeScript
import * as signalR from "@microsoft/signalr";
|
|
import * as Utility from "../Core/Utility";
|
|
|
|
export class SystemHub {
|
|
|
|
private static _instance: SystemHub;
|
|
|
|
public hub: signalR.HubConnection;
|
|
private _connectionState: number;
|
|
|
|
/**
|
|
* Erstellt eine Instanz
|
|
*/
|
|
private constructor() {
|
|
this.init();
|
|
this._connectionState = 0;
|
|
}
|
|
|
|
/**
|
|
* Instanz
|
|
*/
|
|
static getInstance(): SystemHub {
|
|
if (!SystemHub._instance) {
|
|
SystemHub._instance = new SystemHub();
|
|
}
|
|
return SystemHub._instance;
|
|
}
|
|
|
|
/**
|
|
* Initialisierung des Moduls
|
|
*/
|
|
private async init(): Promise<void> {
|
|
var self = this;
|
|
|
|
self.hub = new signalR.HubConnectionBuilder()
|
|
.withUrl("/systemHub")
|
|
.withAutomaticReconnect()
|
|
.build();
|
|
|
|
self.hub.onclose(async () => {
|
|
self._connectionState = 0;
|
|
await self.connectHub();
|
|
});
|
|
|
|
//self.connectHub();
|
|
}
|
|
|
|
/**
|
|
* Verbindung zum Hub starten....
|
|
*/
|
|
public async connectHub(): Promise<void> {
|
|
var self = this;
|
|
if (self._connectionState === 0) {
|
|
await self.hub.start().then(() => {
|
|
self._connectionState = 1;
|
|
}).catch(async e => {
|
|
await Utility.sleep(500);
|
|
if (self._connectionState === 0)
|
|
await self.connectHub();
|
|
});
|
|
}
|
|
}
|
|
} |