Added basic chatting between users

This commit is contained in:
jsrobson10 2019-04-13 16:34:16 +10:00
parent 2d38e4744d
commit 6a78b9d9e2
4 changed files with 88 additions and 19 deletions

View File

@ -227,7 +227,7 @@ function connect(profile, connection_id)
var g = new Object(); var g = new Object();
// Setup some global varibles // Setup some global varibles
g.chats = {}; g.chats = [];
g.users = {}; g.users = {};
// RSA child task // RSA child task
@ -395,6 +395,26 @@ function connect(profile, connection_id)
profile_switch_to(connection_id); profile_switch_to(connection_id);
} }
} }
// New message
if(data.mode == "new_message")
{
console.log("Recieved new message from "+data.from
+" in channel "+data.channel.toString());
// Push the new messages to the chat
g.chats[data.channel].messages.push({
message: data.message,
from: data.from
});
// Is the chat and server active
if(data.channel == active_chat && connection_id == active_profile)
{
// Refresh the chat
chat_switch_to(active_chat);
}
}
} }
}); });
}); });

View File

@ -3,6 +3,7 @@
var profiles; var profiles;
var connections = []; var connections = [];
var active_profile; var active_profile;
var active_chat;
// Load the profiles // Load the profiles
fs.readFile("profiles.json", "utf-8", function(err, data) fs.readFile("profiles.json", "utf-8", function(err, data)
@ -137,8 +138,8 @@ function profile_switch_to(id)
document.getElementById("chat-main").innerHTML = "\ document.getElementById("chat-main").innerHTML = "\
<div id='chats-menu' class='menu-vertical'>\ <div id='chats-menu' class='menu-vertical'>\
<ul>\ <ul>\
<li><a class='add-chat' href='#' onclick='add_chat_menu()'><b>Add</b></a></li>\
"+chats_menu+"\ "+chats_menu+"\
<li><a class='add-chat' href='#' onclick='add_chat_menu()'><b>Add</b></a></li>\
</ul>\ </ul>\
</div>\ </div>\
<div id='chat-area'>\ <div id='chat-area'>\
@ -154,18 +155,52 @@ function profile_switch_to(id)
function chat_switch_to(id) function chat_switch_to(id)
{ {
// Create the chat html
var chat_html = "";
// Loop over the messages
for(var i=0;i<connections[active_profile].chats[id].messages.length;i++)
{
// Add some html data to the chat
chat_html += "\
<div class='chat-content-message'>\
<p>"+connections[active_profile].chats[id].messages[i].message+"</p>\
</div>\
";
}
// Set the HTML // Set the HTML
document.getElementById("chat-area").innerHTML = "\ document.getElementById("chat-area").innerHTML = "\
<div class='chat-content'></div>\ <div id='chat-content'>"+chat_html+"</div>\
<div class='chat-content-send'>\ <div id='chat-content-send'>\
<textarea id='chat-content-send-textbox'></textarea>\ <textarea id='chat-content-send-textarea'></textarea>\
<button id='chat-content-send-button'>Send</button>\ <button id='chat-content-send-button' onclick='chat_send()'>Send</button>\
</div>\ </div>\
"; ";
// Set the title // Set the title
document.title = title(active_profile)+" - " document.title = title(active_profile)+" - "
+connections[active_profile].chats[id].name; +connections[active_profile].chats[id].name;
// Set the active chat
active_chat = id;
}
function chat_send()
{
// Send the message
socket_write(
connections[active_profile].client,
connections[active_profile].encryption,
{
mode: "send_message",
channel: active_chat,
message: document.getElementById("chat-content-send-textarea").value
}
);
// Clear the textarea
document.getElementById("chat-content-send-textarea").value = "";
} }
function add_chat_menu() function add_chat_menu()
@ -211,10 +246,8 @@ function profiles_reload()
// Create a list item // Create a list item
var profile = document.createElement("li"); var profile = document.createElement("li");
// Set the html for the list item // Set the html for the list item
profile.innerHTML = "<a href='javascript:void(0)' onclick='profile_switch_to("+ profile.innerHTML = "<a href='#' onclick='profile_switch_to("+
i+")'>"+title(i)+"</a>"; i+")'>"+title(i)+"</a>";
// Add the profile to the menu // Add the profile to the menu

View File

@ -1,6 +1,14 @@
function connect_direct()
// Check the height of the chat box and adapt the chat area for it
setInterval(function()
{ {
var hostname = document.getElementById("hostname").value; if(
var port = document.getElementById("port").value; // Do these exist yet
connect(hostname, parseInt(port)); document.getElementById("chat-content") &&
document.getElementById("chat-content-send")
){
document.getElementById("chat-content").style.bottom =
document.getElementById("chat-content-send").offsetHeight.toString()+"px";
} }
},
10);

View File

@ -8,8 +8,9 @@
top: 0; top: 0;
bottom: 0; bottom: 0;
left: 240px; left: 240px;
width: 100px; width: 120px;
background-color: #BBB; background-color: #BBB;
overflow-y: auto;
} }
#users-menu ul { #users-menu ul {
@ -18,6 +19,7 @@
right: 0; right: 0;
width: 120px; width: 120px;
background-color: #BBB; background-color: #BBB;
overflow-y: auto;
} }
.menu-vertical ul { .menu-vertical ul {
@ -54,13 +56,19 @@
background-color: #BBC; background-color: #BBC;
}*/ }*/
#chat-area { #chat-content {
padding: 0 100px; top: 0;
bottom: 0;
left: 360px;
right: 120px;
overflow-y: auto;
position: fixed;
padding: 10px;
} }
.chat-content-send { #chat-content-send {
position: fixed; position: fixed;
left: 340px; left: 360px;
right: 120px; right: 120px;
bottom: 0; bottom: 0;
padding: 12px; padding: 12px;