Skip to content

Commit

Permalink
websocket ping pong to verify that sockets can be opened
Browse files Browse the repository at this point in the history
  • Loading branch information
jggoebel committed Jun 21, 2024
1 parent b3435a9 commit 6f763dd
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { AuthGuard } from './auth.guard';
import { StepComponent } from './scenario/step.component';
import { PrintableComponent } from './printable/printable.component';
import { GuacTerminalComponent } from './scenario/guacTerminal.component';
import { WebsocketTestComponent } from './websocket-test/websockettest.component';

const routes: Routes = [
{ path: '', redirectTo: '/app/home', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'test/:url', component: WebsocketTestComponent },
{
path: 'add/:accesscode',
component: AppComponent,
Expand Down
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { VerificationService } from './services/verification.service';
import { TaskProgressComponent } from './scenario/task-progress/task-progress.component';
import { TaskModalComponent } from './scenario/task-modal/task-modal.component';
import { SingleTaskVerificationMarkdownComponent } from './hf-markdown/single-task-verification-markdown/single-task-verification-markdown.component';
import { WebsocketTestComponent } from './websocket-test/websockettest.component';
import '@cds/core/icon/register.js';
import {
ClarityIcons,
Expand Down Expand Up @@ -171,6 +172,7 @@ export function jwtOptionsFactory() {
TaskProgressComponent,
TaskModalComponent,
SingleTaskVerificationMarkdownComponent,
WebsocketTestComponent,
],
imports: [
BrowserModule,
Expand Down
Empty file.
6 changes: 6 additions & 0 deletions src/app/websocket-test/websockettest.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div *ngIf="!success">Healthcheck failed</div>
<div *ngIf="success">Healthcheck Success</div>
<div *ngIf="!socketInProgress && !socketSuccess">
Websocket connection was not established
</div>
<div *ngIf="socketSuccess">Websocket connection established.</div>
57 changes: 57 additions & 0 deletions src/app/websocket-test/websockettest.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
selector: 'app-websockettest',
templateUrl: './websockettest.component.html',
styleUrls: ['./websockettest.component.css'],
})
export class WebsocketTestComponent implements OnInit {
wsEndpoint: string;
endpoint: string;
inProgress: boolean = false;

Check failure on line 13 in src/app/websocket-test/websockettest.component.ts

View workflow job for this annotation

GitHub Actions / Build

Type boolean trivially inferred from a boolean literal, remove type annotation
success: boolean;
socketInProgress: boolean = false;

Check failure on line 15 in src/app/websocket-test/websockettest.component.ts

View workflow job for this annotation

GitHub Actions / Build

Type boolean trivially inferred from a boolean literal, remove type annotation
socketSuccess: boolean;

constructor(private route: ActivatedRoute, private http: HttpClient) {}
ngOnInit(): void {
this.endpoint =
'https://' + this.route.snapshot.params['url'] + '/shell/healthz';
this.wsEndpoint =
'wss://' + this.route.snapshot.params['url'] + '/shell/websocketTest';
this.testConnection();
}

testConnection() {
this.inProgress = true;
this.http.get(this.endpoint).subscribe(() => {
this.success = true;
this.testWSConnection();
this.inProgress = false;
});
}

testWSConnection() {
var socket = new WebSocket(this.wsEndpoint);

Check failure on line 37 in src/app/websocket-test/websockettest.component.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected var, use let or const instead
socket.onmessage = (event) => {
if (event.data == 'pong') {
this.socketSuccess = true;
}
};

socket.onopen = () => {
socket.send('ping');
};

socket.onerror = () => {
this.socketInProgress = true;
};

socket.onerror = () => {
this.socketInProgress = false;
this.socketSuccess = false;
};
}
}

0 comments on commit 6f763dd

Please sign in to comment.