Update websocket.rs and script.js

This commit is contained in:
Wizzard 2025-03-16 00:07:25 -04:00
parent ffa2979da3
commit 5e19f0af47
2 changed files with 50 additions and 7 deletions

@ -10,7 +10,7 @@ use std::io::Write;
use tokio::sync::RwLock;
use tower_http::services::ServeDir;
use crate::comms::RadarData;
use crate::comms::{RadarData, ArcRwlockRadarData};
#[derive(Clone)]
struct AppState {
@ -34,14 +34,25 @@ async fn handle_socket(mut socket: WebSocket, state: AppState) {
if let Ok(json) = serde_json::to_string(&*radar_data) {
compression_buffer.clear();
let mut encoder = GzEncoder::new(Vec::new(), Compression::fast());
let compression_level = if json.len() > 10000 {
Compression::best()
} else {
Compression::fast()
};
let mut encoder = GzEncoder::new(Vec::new(), compression_level);
if encoder.write_all(json.as_bytes()).is_ok() {
match encoder.finish() {
Ok(compressed) => {
if compressed.len() < json.len() {
let mut message = vec![0x01];
message.extend_from_slice(&compressed);
let _ = socket.send(Message::Binary(message)).await;
} else {
let mut uncompressed = vec![0x00];
uncompressed.extend_from_slice(json.as_bytes());
let _ = socket.send(Message::Binary(uncompressed)).await;
}
},
Err(_) => {
let mut uncompressed = vec![0x00];

@ -68,6 +68,35 @@ const websocketAddr = location.protocol === 'https:'
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
const degreesToRadians = (degrees) => degrees * (Math.PI / 180);
const pingTracker = {
history: [],
lastRequestTime: 0,
maxSamples: 10,
startRequest: function () {
this.lastRequestTime = performance.now();
},
endRequest: function () {
if (this.lastRequestTime === 0) return;
const ping = performance.now() - this.lastRequestTime;
this.history.push(ping);
if (this.history.length > this.maxSamples) {
this.history.shift();
}
this.lastRequestTime = 0;
},
getAveragePing: function () {
if (this.history.length === 0) return 0;
const sum = this.history.reduce((a, b) => a + b, 0);
return sum / this.history.length;
}
};
function render() {
requestAnimationFrame(render);
@ -83,6 +112,7 @@ function render() {
if (!isRequestPending && websocket && websocket.readyState === WebSocket.OPEN) {
isRequestPending = true;
pingTracker.startRequest();
websocket.send("requestInfo");
}
@ -117,7 +147,7 @@ function renderFrame() {
ctx.font = "16px Arial";
ctx.textAlign = "left";
ctx.fillStyle = "#00FF00";
ctx.fillText(`${currentFps} FPS | ${freq} Hz`, 10, 20);
ctx.fillText(`${currentFps} FPS | ${freq} Hz | Ping: ${Math.round(pingTracker.getAveragePing())}ms`, 10, 20);
}
}
@ -753,6 +783,8 @@ function processData(data) {
function decompressData(data) {
try {
pingTracker.endRequest();
if (data[0] === 0x01) {
try {
if (typeof pako === 'undefined') {