@@ -131,7 +133,7 @@
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;
+ let serverUrl = prompt(`Enter the WebSocket server URL (default: ${defaultServerUrl}):`) || defaultServerUrl;
// Connect to the WebSocket server
const socket = io(serverUrl);
@@ -140,6 +142,10 @@
const messageInput = document.getElementById('message');
const currentUsernameElement = document.getElementById('currentUsername');
const currentChatRoomElement = document.getElementById('currentChatRoom');
+ const currentServerElement = document.getElementById('currentServer');
+
+ // Set the initial server URL in the top bar
+ currentServerElement.textContent = serverUrl;
// Ask the user for their username and chat room
let username = prompt("Enter your username:") || "Anonymous";
@@ -211,6 +217,38 @@
}
}
+ // Disconnect from the current server
+ function disconnectServer() {
+ if (socket.connected) {
+ socket.disconnect();
+ console.log("Disconnected from the server");
+ alert("You have been disconnected from the server.");
+ currentServerElement.textContent = "Disconnected"; // Update the displayed server URL
+ } else {
+ alert("You are not connected to any server.");
+ }
+ }
+
+ // Change the server to connect to
+ function changeServer() {
+ if (socket.connected) {
+ socket.disconnect();
+ console.log("Disconnected from the current server");
+ }
+
+ const newServerUrl = prompt("Enter the new WebSocket server URL (e.g., http://localhost:5000):");
+ if (newServerUrl && newServerUrl.trim() !== "") {
+ serverUrl = newServerUrl.trim();
+ console.log(`Connecting to new server: ${serverUrl}`);
+ socket.io.uri = serverUrl; // Update the server URL
+ socket.connect(); // Reconnect to the new server
+ currentServerElement.textContent = serverUrl; // Update the displayed server URL
+ alert(`Connected to the new server: ${serverUrl}`);
+ } else {
+ alert("Server URL cannot be empty.");
+ }
+ }
+
// Toggle between light mode and dark mode
function toggleTheme() {
const body = document.body;