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();
@ -64,11 +70,21 @@ function profile_save()
function profile_connect(id) function profile_connect(id)
{ {
// Connect to the server // Is the profile set
var server = connect(profiles[id], connections.length); if(profiles[id])
{
// Connect to the server
var server = connect(profiles[id], connections.length);
// Add the server to the active connections // Add the server to the active connections
connections.push(server); connections.push(server);
}
else
{
// Push an empty connection
connections.push(undefined);
}
} }
function profile_switch_to(id) function profile_switch_to(id)
@ -88,25 +104,29 @@ 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++)
{ {
// Create a list item // Is the profile defined
var profile = document.createElement("li"); if(profiles[i])
// Should the port be displayed
var port_display = "";
// Is the port not the default port
if(profiles[i].port != PORT_DEFAULT)
{ {
// Set the port display varible // Create a list item
port_display = ":"+profiles[i].port.toString(); var profile = document.createElement("li");
// Should the port be displayed
var port_display = "";
// Is the port not the default port
if(profiles[i].port != PORT_DEFAULT)
{
// Set the port display varible
port_display = ":"+profiles[i].port.toString();
}
// Set the html for the list item
profile.innerHTML = "<a href='javascript:void(0)' onclick='profile_switch_to("+
i+")'>"+profiles[i].username+"@"+profiles[i].hostname+port_display+"</a>";
// Add the profile to the menu
chat_menu.appendChild(profile);
} }
// Set the html for the list item
profile.innerHTML = "<a href='javascript:void(0)' onclick='profile_switch_to("+
i+")'>"+profiles[i].username+"@"+profiles[i].hostname+port_display+"</a>";
// Add the profile to the menu
chat_menu.appendChild(profile);
} }
} }