Added reciving chat/user data from the server

This commit is contained in:
jsrobson10 2019-04-12 17:17:16 +10:00
parent a5f1b53a34
commit f2231cd43c
2 changed files with 96 additions and 24 deletions

View File

@ -226,6 +226,10 @@ function connect(profile, connection_id)
// Make an accessible global object // Make an accessible global object
var g = new Object(); var g = new Object();
// Setup some global varibles
g.chats = {};
g.users = {};
// RSA child task // RSA child task
g.rsa_task = child_process.fork( g.rsa_task = child_process.fork(
path.resolve('rsa.js'), [], path.resolve('rsa.js'), [],
@ -324,17 +328,65 @@ function connect(profile, connection_id)
// Convert it from JSON // Convert it from JSON
data = JSON.parse(data); 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) client.on('close', function(data)
{ {
console.log("Connection closed:", connection_id)
// Close the connection // Close the connection
delete connections[connection_id]; 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) g.rsa_task.on('message', function(message)

View File

@ -36,6 +36,12 @@ fs.readFile("profiles.json", "utf-8", function(err, data)
document.getElementById("chat-menu-config").style.height; 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() function profile_save()
{ {
// Get the values from the form // Get the values from the form
@ -53,7 +59,7 @@ function profile_save()
}); });
// Write the data to the profiles file // Write the data to the profiles file
fs.writeFileSync("profiles.json", JSON.stringify(profiles)); profiles_export();
// Reload the profiles // Reload the profiles
profiles_reload(); profiles_reload();
@ -63,6 +69,9 @@ function profile_save()
} }
function profile_connect(id) function profile_connect(id)
{
// Is the profile set
if(profiles[id])
{ {
// Connect to the server // Connect to the server
var server = connect(profiles[id], connections.length); var server = connect(profiles[id], connections.length);
@ -71,6 +80,13 @@ function profile_connect(id)
connections.push(server); connections.push(server);
} }
else
{
// Push an empty connection
connections.push(undefined);
}
}
function profile_switch_to(id) function profile_switch_to(id)
{ {
// Set the active profile // Set the active profile
@ -87,6 +103,9 @@ function profiles_reload()
// Loop over the profiles // Loop over the profiles
for(var i=0;i<profiles.length;i++) for(var i=0;i<profiles.length;i++)
{
// Is the profile defined
if(profiles[i])
{ {
// Create a list item // Create a list item
var profile = document.createElement("li"); var profile = document.createElement("li");
@ -109,6 +128,7 @@ function profiles_reload()
chat_menu.appendChild(profile); chat_menu.appendChild(profile);
} }
} }
}
function switch_new_profile() function switch_new_profile()
{ {