test connection UI (#4596)

* test connection UI

* cleanup

Co-authored-by: Jeremy Letto <jeremy.letto@datasite.com>
This commit is contained in:
Jeremy Letto 2022-03-19 19:22:00 -05:00 committed by GitHub
parent 00a2a8ab71
commit 0ff59e6d1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 169 additions and 22 deletions

View file

@ -38,6 +38,16 @@ export class WebSocketService {
this.socket = this.createWebSocket(`${protocol}://${host}:${port}`);
}
public testConnect(options: WebSocketConnectOptions, protocol: string = 'wss'): void {
if (window.location.hostname === 'localhost') {
protocol = 'ws';
}
const { host, port } = options;
this.testWebSocket(`${protocol}://${host}:${port}`);
}
public disconnect(): void {
if (this.socket) {
this.socket.close();
@ -92,4 +102,21 @@ export class WebSocketService {
return socket;
}
private testWebSocket(url: string): void {
const socket = new WebSocket(url);
socket.binaryType = 'arraybuffer';
const connectionTimer = setTimeout(() => socket.close(), this.webClient.clientOptions.keepalive);
socket.onopen = () => {
clearTimeout(connectionTimer);
SessionPersistence.testConnectionSuccessful();
socket.close();
};
socket.onerror = () => {
SessionPersistence.testConnectionFailed();
};
}
}