Skip to content

Commit 4938a4a

Browse files
foxbit19jmattheis
authored andcommitted
Add auto reconnect on connection loss (#228)
1 parent 7cf5c55 commit 4938a4a

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

ui/src/CurrentUser.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const tokenKey = 'gotify-login-key';
1010

1111
export class CurrentUser {
1212
private tokenCache: string | null = null;
13+
private reconnectTimeoutId: number | null = null;
14+
private readonly reconnectTime: number = 3000;
1315
@observable
1416
public loggedIn = false;
1517
@observable
@@ -90,7 +92,7 @@ export class CurrentUser {
9092
})
9193
.catch((error: AxiosError) => {
9294
if (!error || !error.response) {
93-
this.hasNetwork = false;
95+
this.lostNetwork();
9496
return Promise.reject(error);
9597
}
9698

@@ -124,4 +126,23 @@ export class CurrentUser {
124126
.post(config.get('url') + 'current/user/password', {pass})
125127
.then(() => this.snack('Password changed'));
126128
};
129+
130+
public tryReconnect = (quiet = false) => {
131+
this.tryAuthenticate().catch(() => {
132+
if (!quiet) {
133+
this.snack('Reconnect failed');
134+
}
135+
});
136+
};
137+
138+
private lostNetwork = () => {
139+
this.hasNetwork = false;
140+
if (this.reconnectTimeoutId !== null) {
141+
window.clearTimeout(this.reconnectTimeoutId);
142+
}
143+
this.reconnectTimeoutId = window.setTimeout(
144+
() => this.tryReconnect(true),
145+
this.reconnectTime
146+
);
147+
};
127148
}

ui/src/layout/Layout.tsx

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ class Layout extends React.Component<
6363
private showSettings = false;
6464
@observable
6565
private version = Layout.defaultVersion;
66-
@observable
67-
private reconnecting = false;
6866

6967
public componentDidMount() {
7068
if (this.version === Layout.defaultVersion) {
@@ -81,19 +79,6 @@ class Layout extends React.Component<
8179
}
8280
}
8381

84-
private doReconnect = () => {
85-
this.reconnecting = true;
86-
this.props.currentUser
87-
.tryAuthenticate()
88-
.then(() => {
89-
this.reconnecting = false;
90-
})
91-
.catch(() => {
92-
this.reconnecting = false;
93-
this.props.snackManager.snack('Reconnect failed');
94-
});
95-
};
96-
9782
public render() {
9883
const {version, showSettings, currentTheme} = this;
9984
const {
@@ -104,6 +89,7 @@ class Layout extends React.Component<
10489
user: {name, admin},
10590
logout,
10691
hasNetwork,
92+
tryReconnect,
10793
},
10894
} = this.props;
10995
const theme = themeMap[currentTheme];
@@ -113,7 +99,7 @@ class Layout extends React.Component<
11399
<HashRouter>
114100
<div>
115101
{hasNetwork ? null : (
116-
<NetworkLostBanner height={64} retry={this.doReconnect} />
102+
<NetworkLostBanner height={64} retry={() => tryReconnect()} />
117103
)}
118104
<div style={{display: 'flex'}}>
119105
<CssBaseline />
@@ -131,7 +117,7 @@ class Layout extends React.Component<
131117

132118
<main className={classes.content}>
133119
<Switch>
134-
{authenticating || this.reconnecting ? (
120+
{authenticating ? (
135121
<Route path="/">
136122
<LoadingSpinner />
137123
</Route>

0 commit comments

Comments
 (0)