Added reciving chat/user data from the server
This commit is contained in:
parent
a5f1b53a34
commit
f2231cd43c
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
// Push an empty connection
|
||||
connections.push(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
function profile_switch_to(id)
|
||||
|
@ -87,6 +103,9 @@ function profiles_reload()
|
|||
|
||||
// Loop over the profiles
|
||||
for(var i=0;i<profiles.length;i++)
|
||||
{
|
||||
// Is the profile defined
|
||||
if(profiles[i])
|
||||
{
|
||||
// Create a list item
|
||||
var profile = document.createElement("li");
|
||||
|
@ -108,6 +127,7 @@ function profiles_reload()
|
|||
// Add the profile to the menu
|
||||
chat_menu.appendChild(profile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function switch_new_profile()
|
||||
|
|
Loading…
Reference in New Issue