From 2b2dfdfba60dfbb34b1fdad5b4da6fcfa7de862b Mon Sep 17 00:00:00 2001 From: ITQ Date: Fri, 25 Jul 2025 17:29:03 +0300 Subject: [PATCH] fix(loadtest): fixed browser security error --- services/loadtest/static/index.html | 81 ++++++++++++----------------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/services/loadtest/static/index.html b/services/loadtest/static/index.html index f30492e..6588637 100644 --- a/services/loadtest/static/index.html +++ b/services/loadtest/static/index.html @@ -263,61 +263,48 @@ function setupWebSocket () { - let wsUrl = 'ws://' + window.location.host + '/ws' - let triedSecure = false + const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://' + const wsUrl = protocol + window.location.host + '/ws' + ws = new WebSocket( wsUrl ) - function connect ( url ) + ws.onopen = () => console.log( 'WebSocket connected' ) + ws.onclose = () => console.log( 'WebSocket disconnected' ) + ws.onerror = ( error ) => console.error( 'WebSocket error:', error ) + + ws.onmessage = ( event ) => { - ws = new WebSocket( url ) - ws.onopen = () => console.log( 'WebSocket connected' ) - ws.onclose = () => console.log( 'WebSocket disconnected' ) - ws.onerror = ( error ) => - { - console.error( 'WebSocket error:', error ) - if ( !triedSecure ) - { - triedSecure = true - wsUrl = 'wss://' + window.location.host + '/ws' - connect( wsUrl ) - } - } - ws.onmessage = ( event ) => - { - const data = JSON.parse( event.data ) - - if ( typeof data.isRunning !== 'undefined' ) - { - if ( data.isRunning ) - { - isRunning = true - startStopBtn.textContent = 'Stop Test' - startStopBtn.classList.remove( 'bg-green-600', 'hover:bg-green-500' ) - startStopBtn.classList.add( 'bg-red-600', 'hover:bg-red-500' ) - configPanel.querySelectorAll( 'input, select' ).forEach( el => el.disabled = true ) - } - } + const data = JSON.parse( event.data ) + if ( typeof data.isRunning !== 'undefined' ) + { if ( data.isRunning ) { - const now = new Date().toLocaleTimeString() - rpsStat.textContent = data.rps - latencyStat.textContent = data.latency.toFixed( 2 ) - errorRateStat.textContent = data.errorRate.toFixed( 2 ) + '%' - totalReqsStat.textContent = data.totalReqs - totalErrorsStat.textContent = data.totalErrors - - updateChart( rpsChart, now, data.rps ) - updateChart( latencyChart, now, data.latency ) - updateChart( errorRateChart, now, data.errorRate ) - } else - { - totalReqsStat.textContent = data.totalReqs - totalErrorsStat.textContent = data.totalErrors + isRunning = true + startStopBtn.textContent = 'Stop Test' + startStopBtn.classList.remove( 'bg-green-600', 'hover:bg-green-500' ) + startStopBtn.classList.add( 'bg-red-600', 'hover:bg-red-500' ) + configPanel.querySelectorAll( 'input, select' ).forEach( el => el.disabled = true ) } } - } - connect( wsUrl ) + if ( data.isRunning ) + { + const now = new Date().toLocaleTimeString() + rpsStat.textContent = data.rps + latencyStat.textContent = data.latency.toFixed( 2 ) + errorRateStat.textContent = data.errorRate.toFixed( 2 ) + '%' + totalReqsStat.textContent = data.totalReqs + totalErrorsStat.textContent = data.totalErrors + + updateChart( rpsChart, now, data.rps ) + updateChart( latencyChart, now, data.latency ) + updateChart( errorRateChart, now, data.errorRate ) + } else + { + totalReqsStat.textContent = data.totalReqs + totalErrorsStat.textContent = data.totalErrors + } + } } function setRunningState ()