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

@ -57,7 +57,11 @@ export class WebClient {
public connect(options: WebSocketConnectOptions) {
this.connectionAttemptMade = true;
this.options = options;
this.socket.connect(this.options);
this.socket.connect(options);
}
public testConnect(options: WebSocketConnectOptions) {
this.socket.testConnect(options);
}
public disconnect() {

View file

@ -25,6 +25,9 @@ export class SessionCommands {
case WebSocketConnectReason.PASSWORD_RESET:
SessionCommands.updateStatus(StatusEnum.CONNECTING, 'Connecting...');
break;
case WebSocketConnectReason.TEST_CONNECTION:
webClient.testConnect({ ...options });
return;
default:
SessionCommands.updateStatus(StatusEnum.DISCONNECTED, 'Unknown Connection Attempt: ' + reason);
return;

View file

@ -29,6 +29,14 @@ export class SessionPersistence {
ServerDispatch.connectionFailed();
}
static testConnectionSuccessful() {
ServerDispatch.testConnectionSuccessful();
}
static testConnectionFailed() {
ServerDispatch.testConnectionFailed();
}
static updateBuddyList(buddyList) {
ServerDispatch.updateBuddyList(buddyList);
}

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();
};
}
}