Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2afd8c3

Browse files
refactor signalr.service.ts
1 parent d5a2e40 commit 2afd8c3

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

‎GhostUI/ClientApp/src/api/signalr.service.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventBus } from '@/event-bus';
22
import { SIGNALR_CONFIG } from '../config';
3-
import { HubConnection, HubConnectionBuilder, HubConnectionState } from '@microsoft/signalr';
3+
import { HubConnection, HubConnectionBuilder, HubConnectionState,LogLevel } from '@microsoft/signalr';
44

55
/**
66
* SignalR API abstraction layer communication.
@@ -19,15 +19,26 @@ class SignalRService {
1919
return this._signalRService || (this._signalRService = new this());
2020
}
2121

22-
public startConnection(): void {
23-
if (this._hubConnection.state === HubConnectionState.Disconnected) {
24-
this._hubConnection.start().catch((e) => console.error(e));
22+
get connectionState(): HubConnectionState {
23+
return this._hubConnection?.state ?? HubConnectionState.Disconnected;
24+
}
25+
26+
public async startConnection(): Promise<void> {
27+
try {
28+
await this._hubConnection?.start();
29+
console.assert(this.connectionState === HubConnectionState.Connected);
30+
} catch (e) {
31+
console.assert(this.connectionState === HubConnectionState.Disconnected);
32+
console.error(e);
33+
setTimeout(() => this.startConnection(), 5000);
2534
}
2635
}
2736

2837
private createConnection(): void {
2938
this._hubConnection = new HubConnectionBuilder()
3039
.withUrl(SIGNALR_CONFIG.baseUrl)
40+
.withAutomaticReconnect()
41+
.configureLogging(LogLevel.Information)
3142
.build();
3243
}
3344

@@ -50,11 +61,13 @@ class SignalRService {
5061
this.hubToastMessage('A user has logged out');
5162
});
5263

53-
this._hubConnection.on(SIGNALR_CONFIG.events.closeConnections, (reason: string) => {
54-
this._hubConnection.stop()
55-
.then(() => {
56-
this.hubToastMessage(`Hub closed (${reason})`);
57-
});
64+
this._hubConnection.on(SIGNALR_CONFIG.events.closeConnections, async (reason: string) => {
65+
try {
66+
await this._hubConnection.stop();
67+
this.hubToastMessage(`Hub closed (${reason})`);
68+
} catch (e) {
69+
console.error(e);
70+
}
5871
});
5972
}
6073
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /