mirror of
https://github.com/jwetzell/showbridge-webui.git
synced 2026-08-21 05:59:00 +00:00
handle pong event and show round trip latency
This commit is contained in:
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
<div class="w-full">
|
<div class="w-full">
|
||||||
<mat-toolbar color="primary">
|
<mat-toolbar color="primary">
|
||||||
@if (eventsService.status() === 'open') {
|
@if (eventsService.status() === 'open') {
|
||||||
<div matTooltip="Connected" class="h-3 w-3 rounded-full bg-green-400 mr-2"></div>
|
<div [matTooltip]="'Connected' + (eventsService.socketRoundTripLatency() !== null ? ' (' + eventsService.socketRoundTripLatency() + ' ms)' : '')" class="h-3 w-3 rounded-full bg-green-400 mr-2"></div>
|
||||||
} @else if (eventsService.status() === 'closed') {
|
} @else if (eventsService.status() === 'closed') {
|
||||||
<div matTooltip="Disconnected" class="h-3 w-3 rounded-full bg-orange-400 mr-2"></div>
|
<div matTooltip="Disconnected" class="h-3 w-3 rounded-full bg-orange-400 mr-2"></div>
|
||||||
} @else if (eventsService.status() === 'error') {
|
} @else if (eventsService.status() === 'error') {
|
||||||
|
|||||||
@@ -13,9 +13,13 @@ export class EventsService {
|
|||||||
private outputEvents$ = new Subject<RouterEvent<'output', OutputEventData>>();
|
private outputEvents$ = new Subject<RouterEvent<'output', OutputEventData>>();
|
||||||
private settingsService = inject(SettingsService);
|
private settingsService = inject(SettingsService);
|
||||||
|
|
||||||
|
private pingInterval?: number;
|
||||||
|
private lastPingTimestamp?: number;
|
||||||
|
|
||||||
|
public socketRoundTripLatency = signal<number | null>(null);
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
effect(() => {
|
effect(() => {
|
||||||
console.log('Websocket URL changed:', this.settingsService.wsUrl());
|
|
||||||
if (this.socket) {
|
if (this.socket) {
|
||||||
this.socket.close();
|
this.socket.close();
|
||||||
}
|
}
|
||||||
@@ -28,6 +32,15 @@ export class EventsService {
|
|||||||
this.socket = new WebSocket(this.settingsService.wsUrl());
|
this.socket = new WebSocket(this.settingsService.wsUrl());
|
||||||
this.socket.onopen = () => {
|
this.socket.onopen = () => {
|
||||||
this.status.set('open');
|
this.status.set('open');
|
||||||
|
if (this.pingInterval) {
|
||||||
|
clearInterval(this.pingInterval);
|
||||||
|
}
|
||||||
|
this.pingInterval = setInterval(() => {
|
||||||
|
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
|
||||||
|
this.lastPingTimestamp = Date.now();
|
||||||
|
this.socket.send(JSON.stringify({ type: 'ping', data: {timestamp: this.lastPingTimestamp} }));
|
||||||
|
}
|
||||||
|
}, 5000);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.socket.onclose = () => {
|
this.socket.onclose = () => {
|
||||||
@@ -54,6 +67,11 @@ export class EventsService {
|
|||||||
case 'output':
|
case 'output':
|
||||||
this.outputEvents$.next(messageObj);
|
this.outputEvents$.next(messageObj);
|
||||||
break;
|
break;
|
||||||
|
case 'pong':
|
||||||
|
if (this.lastPingTimestamp) {
|
||||||
|
this.socketRoundTripLatency.set(Date.now() - this.lastPingTimestamp);
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
console.warn('Unknown event type:', messageObj.type);
|
console.warn('Unknown event type:', messageObj.type);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user