- Implemented a new Player class in player.js to handle audio playback, including HLS support using hls.js. - Created a shared API module in api.js for making HTTP requests with proper error handling. - Added DOM utility functions in dom.js for creating and clearing elements. - Introduced WebSocket connection handling in ws.js for real-time updates. - Developed a comprehensive CSS stylesheet for styling the application, including a high-contrast theme.
23 lines
755 B
JavaScript
23 lines
755 B
JavaScript
export function connectWs(onMessage) {
|
|
let ws, retry = 0, closed = false;
|
|
function open() {
|
|
const proto = location.protocol === 'https:' ? 'wss' : 'ws';
|
|
ws = new WebSocket(`${proto}://${location.host}/ws`);
|
|
ws.addEventListener('open', () => { retry = 0; });
|
|
ws.addEventListener('message', (ev) => {
|
|
try { onMessage(JSON.parse(ev.data)); } catch {}
|
|
});
|
|
ws.addEventListener('close', () => {
|
|
if (closed) return;
|
|
retry = Math.min(retry + 1, 6);
|
|
setTimeout(open, 500 * 2 ** retry);
|
|
});
|
|
ws.addEventListener('error', () => ws.close());
|
|
}
|
|
open();
|
|
return {
|
|
send(msg) { if (ws?.readyState === WebSocket.OPEN) ws.send(JSON.stringify(msg)); },
|
|
close() { closed = true; ws?.close(); }
|
|
};
|
|
}
|