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
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)

View File

@ -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.length;i++)
{
// Create a list item
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)
// Is the profile defined
if(profiles[i])
{
// Set the port display varible
port_display = ":"+profiles[i].port.toString();
// Create a list item
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);
}
}