Allow the webclient to have a default for the WebSocket server to default to where it's hosted.

This commit is contained in:
Charles Descamps 2025-03-28 17:46:25 -07:00
parent 2c9a3bc68a
commit 38e5049447

View file

@ -106,7 +106,7 @@
}
</style>
</head>
<body class="light-mode">
<body class="dark-mode">
<!-- Top Bar -->
<div id="topBar">
<span id="userInfo">Username: <strong id="currentUsername"></strong> | Chat Room: <strong id="currentChatRoom"></strong></span>
@ -127,9 +127,14 @@
</div>
<script>
// Ask the user for the server URL
const serverUrl = prompt("Enter the WebSocket server URL (e.g., http://localhost:5000):") || 'http://localhost:5000';
const socket = io(serverUrl); // Connect to the WebSocket server
// Determine the default server URL based on the web client's host
const defaultServerUrl = `${window.location.protocol}//${window.location.hostname}:5000`;
// Ask the user for the server URL, defaulting to the web client's host
const serverUrl = prompt(`Enter the WebSocket server URL (default: ${defaultServerUrl}):`) || defaultServerUrl;
// Connect to the WebSocket server
const socket = io(serverUrl);
const chatDiv = document.getElementById('chat');
const messageInput = document.getElementById('message');
@ -212,11 +217,17 @@
if (body.classList.contains('light-mode')) {
body.classList.remove('light-mode');
body.classList.add('dark-mode');
localStorage.setItem('theme', 'dark-mode'); // Save preference
} else {
body.classList.remove('dark-mode');
body.classList.add('light-mode');
localStorage.setItem('theme', 'light-mode'); // Save preference
}
}
// Apply saved theme on page load
const savedTheme = localStorage.getItem('theme') || 'dark-mode'; // Default to dark mode
document.body.classList.add(savedTheme);
</script>
</body>
</html>