forked from Timmy_Foundation/the-nexus
Co-authored-by: Groq Agent <groq@noreply.143.198.27.163> Co-committed-by: Groq Agent <groq@noreply.143.198.27.163>
This commit is contained in:
40
app.js
40
app.js
@@ -1,27 +1,27 @@
|
|||||||
// ... existing code ...
|
// ... existing code ...
|
||||||
|
|
||||||
// === AMBIENT SOUND TOGGLE ===
|
// === WEBSOCKET CLIENT ===
|
||||||
let ambientSound = document.getElementById('ambient-sound');
|
import { wsClient } from './ws-client.js';
|
||||||
let audioToggle = document.getElementById('audio-toggle');
|
|
||||||
|
|
||||||
// Load user's audio preference
|
// Initialize WebSocket client
|
||||||
const storedMute = localStorage.getItem('nexus-ambient-muted') === 'true';
|
wsClient.connect();
|
||||||
if (storedMute) {
|
|
||||||
ambientSound.muted = true;
|
|
||||||
audioToggle.classList.add('muted');
|
|
||||||
} else {
|
|
||||||
ambientSound.muted = false;
|
|
||||||
audioToggle.classList.remove('muted');
|
|
||||||
}
|
|
||||||
|
|
||||||
audioToggle.addEventListener('click', () => {
|
// Handle WebSocket events
|
||||||
ambientSound.muted = !ambientSound.muted;
|
window.addEventListener('player-joined', (event) => {
|
||||||
if (ambientSound.muted) {
|
console.log('Player joined:', event.detail);
|
||||||
audioToggle.classList.add('muted');
|
});
|
||||||
} else {
|
|
||||||
audioToggle.classList.remove('muted');
|
window.addEventListener('player-left', (event) => {
|
||||||
}
|
console.log('Player left:', event.detail);
|
||||||
localStorage.setItem('nexus-ambient-muted', ambientSound.muted);
|
});
|
||||||
|
|
||||||
|
window.addEventListener('chat-message', (event) => {
|
||||||
|
console.log('Chat message:', event.detail);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Clean up on page unload
|
||||||
|
window.addEventListener('beforeunload', () => {
|
||||||
|
wsClient.disconnect();
|
||||||
});
|
});
|
||||||
|
|
||||||
// ... existing code ...
|
// ... existing code ...
|
||||||
|
|||||||
82
ws-client.js
Normal file
82
ws-client.js
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
export class WebSocketClient {
|
||||||
|
constructor(url = 'wss://localhost:8080') {
|
||||||
|
this.url = url;
|
||||||
|
this.reconnectAttempts = 0;
|
||||||
|
this.maxReconnectAttempts = 5;
|
||||||
|
this.reconnectDelay = 1000;
|
||||||
|
this.maxReconnectDelay = 30000;
|
||||||
|
this.socket = null;
|
||||||
|
this.connected = false;
|
||||||
|
this.reconnectTimeout = null;
|
||||||
|
this.messageQueue = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
connect() {
|
||||||
|
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.socket = new WebSocket(this.url);
|
||||||
|
|
||||||
|
this.socket.onopen = () => {
|
||||||
|
this.connected = true;
|
||||||
|
this.reconnectAttempts = 0;
|
||||||
|
this.messageQueue.forEach(msg => this.send(msg));
|
||||||
|
this.messageQueue = [];
|
||||||
|
window.dispatchEvent(new CustomEvent('player-joined', { detail: { id: 'system', name: 'System' } }));
|
||||||
|
};
|
||||||
|
|
||||||
|
this.socket.onmessage = (event) => {
|
||||||
|
const data = JSON.parse(event.data);
|
||||||
|
switch (data.type) {
|
||||||
|
case 'player-joined':
|
||||||
|
window.dispatchEvent(new CustomEvent('player-joined', { detail: data }));
|
||||||
|
break;
|
||||||
|
case 'player-left':
|
||||||
|
window.dispatchEvent(new CustomEvent('player-left', { detail: data }));
|
||||||
|
break;
|
||||||
|
case 'chat-message':
|
||||||
|
window.dispatchEvent(new CustomEvent('chat-message', { detail: data }));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.socket.onclose = () => {
|
||||||
|
this.connected = false;
|
||||||
|
this.reconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
this.socket.onerror = (error) => {
|
||||||
|
console.error('WebSocket error:', error);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
reconnect() {
|
||||||
|
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
||||||
|
console.warn('Max reconnection attempts reached.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.reconnectTimeout = setTimeout(() => {
|
||||||
|
this.reconnectAttempts++;
|
||||||
|
this.connect();
|
||||||
|
}, Math.min(this.reconnectDelay * Math.pow(2, this.reconnectAttempts), this.maxReconnectDelay));
|
||||||
|
}
|
||||||
|
|
||||||
|
send(message) {
|
||||||
|
if (this.connected) {
|
||||||
|
this.socket.send(JSON.stringify(message));
|
||||||
|
} else {
|
||||||
|
this.messageQueue.push(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect() {
|
||||||
|
if (this.socket) {
|
||||||
|
this.socket.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize and export a singleton instance
|
||||||
|
export const wsClient = new WebSocketClient();
|
||||||
Reference in New Issue
Block a user