diff --git a/scripts/communications.js b/scripts/communications.js index cd9a286..8b58bc7 100644 --- a/scripts/communications.js +++ b/scripts/communications.js @@ -226,6 +226,10 @@ function connect(profile, connection_id) // Make an accessible global object var g = new Object(); + // Setup some global varibles + g.chats = {}; + g.users = {}; + // RSA child task g.rsa_task = child_process.fork( path.resolve('rsa.js'), [], @@ -324,17 +328,65 @@ function connect(profile, connection_id) // Convert it from JSON data = JSON.parse(data); - console.log("From server:", data); + // Is this an error + if(data.mode == "error") + { + // Is this an authentication error + if(data.error == "auth") + { + // Destroy the connection + client.destroy(); - // Note to self: do something here + // Delete the connections + delete connections[connection_id]; + delete profiles[connection_id]; + + // Update the profiles + profiles_export(); + profiles_reload(); + } + } + + // Logged in + if(data.mode == "login") + { + console.log("Recieved details:", data.chats, data.users); + + // Save the varibles sent + g.users = data.users; + g.chats = data.chats; + + // Switch to this chat + profile_switch_to(connection_id); + } } }); }); client.on('close', function(data) { + console.log("Connection closed:", connection_id) + // Close the connection delete connections[connection_id]; + + // Attempt to restart the connection in 10 seconds + console.log("Attempting reconnection...") + setTimeout(function() + { + // Attempt reconnection + console.log("Attempted reconection."); + try + { + profile_connect(connection_id); + } + + catch(e) + { + console.log("Caught error", e); + } + }, + 1000); }); g.rsa_task.on('message', function(message) diff --git a/scripts/profiles.js b/scripts/profiles.js index 10781d3..a9fdcfa 100644 --- a/scripts/profiles.js +++ b/scripts/profiles.js @@ -36,6 +36,12 @@ fs.readFile("profiles.json", "utf-8", function(err, data) document.getElementById("chat-menu-config").style.height; }); +function profiles_export() +{ + // Write the data to the profiles file + fs.writeFileSync("profiles.json", JSON.stringify(profiles)); +} + function profile_save() { // Get the values from the form @@ -53,7 +59,7 @@ function profile_save() }); // Write the data to the profiles file - fs.writeFileSync("profiles.json", JSON.stringify(profiles)); + profiles_export(); // Reload the profiles profiles_reload(); @@ -64,11 +70,21 @@ function profile_save() function profile_connect(id) { - // Connect to the server - var server = connect(profiles[id], connections.length); + // Is the profile set + if(profiles[id]) + { + // Connect to the server + var server = connect(profiles[id], connections.length); - // Add the server to the active connections - connections.push(server); + // Add the server to the active connections + connections.push(server); + } + + else + { + // Push an empty connection + connections.push(undefined); + } } function profile_switch_to(id) @@ -88,25 +104,29 @@ function profiles_reload() // Loop over the profiles for(var i=0;i"+profiles[i].username+"@"+profiles[i].hostname+port_display+""; + + // Add the profile to the menu + chat_menu.appendChild(profile); } - - // Set the html for the list item - profile.innerHTML = ""+profiles[i].username+"@"+profiles[i].hostname+port_display+""; - - // Add the profile to the menu - chat_menu.appendChild(profile); } }