skyview/assets/static/js/modules/websocket.js
Ole-Morten Duesund 1de3e092ae Fix aircraft markers not updating positions in real-time
Root cause: The merger was blocking position updates from the same source
after the first position was established, designed for multi-source scenarios
but preventing single-source position updates.

Changes:
- Refactor JavaScript into modular architecture (WebSocketManager, AircraftManager, MapManager, UIManager)
- Add CPR coordinate validation to prevent invalid latitude/longitude values
- Fix merger to allow position updates from same source for moving aircraft
- Add comprehensive coordinate bounds checking in CPR decoder
- Update HTML to use new modular JavaScript with cache busting
- Add WebSocket debug logging to track data flow

Technical details:
- CPR decoder now validates coordinates within ±90° latitude, ±180° longitude
- Merger allows updates when currentBest == sourceID (same source continuous updates)
- JavaScript modules provide better separation of concerns and debugging
- WebSocket properly transmits updated aircraft coordinates to frontend

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-24 14:04:17 +02:00

70 lines
No EOL
2.6 KiB
JavaScript

// WebSocket communication module
export class WebSocketManager {
constructor(onMessage, onStatusChange) {
this.websocket = null;
this.onMessage = onMessage;
this.onStatusChange = onStatusChange;
}
async connect() {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${protocol}//${window.location.host}/ws`;
try {
this.websocket = new WebSocket(wsUrl);
this.websocket.onopen = () => {
console.log('WebSocket connected');
this.onStatusChange('connected');
};
this.websocket.onclose = () => {
console.log('WebSocket disconnected');
this.onStatusChange('disconnected');
// Reconnect after 5 seconds
setTimeout(() => this.connect(), 5000);
};
this.websocket.onerror = (error) => {
console.error('WebSocket error:', error);
this.onStatusChange('disconnected');
};
this.websocket.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
// Debug: Log WebSocket messages to see what we're receiving
if (message.data && message.data.aircraft) {
const aircraftCount = Object.keys(message.data.aircraft).length;
console.log(`📡 WebSocket: ${message.type} with ${aircraftCount} aircraft`);
// Log first few aircraft with coordinates
let count = 0;
for (const [icao, aircraft] of Object.entries(message.data.aircraft)) {
if (count < 3 && aircraft.Latitude && aircraft.Longitude) {
console.log(`📡 ${icao}: lat=${aircraft.Latitude}, lon=${aircraft.Longitude}`);
}
count++;
}
}
this.onMessage(message);
} catch (error) {
console.error('Failed to parse WebSocket message:', error);
}
};
} catch (error) {
console.error('WebSocket connection failed:', error);
this.onStatusChange('disconnected');
}
}
disconnect() {
if (this.websocket) {
this.websocket.close();
this.websocket = null;
}
}
}