From 6a78b9d9e2ca1dffc4acd0488bfcfda520d5093c Mon Sep 17 00:00:00 2001 From: jsrobson10 Date: Sat, 13 Apr 2019 16:34:16 +1000 Subject: [PATCH] Added basic chatting between users --- scripts/communications.js | 22 +++++++++++++++++- scripts/profiles.js | 49 ++++++++++++++++++++++++++++++++------- scripts/scripts.js | 18 ++++++++++---- style.css | 18 ++++++++++---- 4 files changed, 88 insertions(+), 19 deletions(-) diff --git a/scripts/communications.js b/scripts/communications.js index 66ffb52..2366b07 100644 --- a/scripts/communications.js +++ b/scripts/communications.js @@ -227,7 +227,7 @@ function connect(profile, connection_id) var g = new Object(); // Setup some global varibles - g.chats = {}; + g.chats = []; g.users = {}; // RSA child task @@ -395,6 +395,26 @@ function connect(profile, 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); + } + } } }); }); diff --git a/scripts/profiles.js b/scripts/profiles.js index e1b6ec9..304e2ca 100644 --- a/scripts/profiles.js +++ b/scripts/profiles.js @@ -3,6 +3,7 @@ var profiles; var connections = []; var active_profile; +var active_chat; // Load the profiles fs.readFile("profiles.json", "utf-8", function(err, data) @@ -137,8 +138,8 @@ function profile_switch_to(id) document.getElementById("chat-main").innerHTML = "\ \
\ @@ -154,18 +155,52 @@ function profile_switch_to(id) function chat_switch_to(id) { + // Create the chat html + var chat_html = ""; + + // Loop over the messages + for(var i=0;i\ +
\ + "; + } + // Set the HTML document.getElementById("chat-area").innerHTML = "\ -
\ -
\ - \ - \ +
"+chat_html+"
\ +
\ + \ + \
\ "; // Set the title document.title = title(active_profile)+" - " +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() @@ -211,10 +246,8 @@ function profiles_reload() // Create a list item var profile = document.createElement("li"); - - // Set the html for the list item - profile.innerHTML = ""+title(i)+""; // Add the profile to the menu diff --git a/scripts/scripts.js b/scripts/scripts.js index 3866df0..836452c 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -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; - var port = document.getElementById("port").value; - connect(hostname, parseInt(port)); -} + if( + // Do these exist yet + document.getElementById("chat-content") && + document.getElementById("chat-content-send") + ){ + document.getElementById("chat-content").style.bottom = + document.getElementById("chat-content-send").offsetHeight.toString()+"px"; + } +}, +10); diff --git a/style.css b/style.css index 281654b..804ab0d 100644 --- a/style.css +++ b/style.css @@ -8,8 +8,9 @@ top: 0; bottom: 0; left: 240px; - width: 100px; + width: 120px; background-color: #BBB; + overflow-y: auto; } #users-menu ul { @@ -18,6 +19,7 @@ right: 0; width: 120px; background-color: #BBB; + overflow-y: auto; } .menu-vertical ul { @@ -54,13 +56,19 @@ background-color: #BBC; }*/ -#chat-area { - padding: 0 100px; +#chat-content { + top: 0; + bottom: 0; + left: 360px; + right: 120px; + overflow-y: auto; + position: fixed; + padding: 10px; } -.chat-content-send { +#chat-content-send { position: fixed; - left: 340px; + left: 360px; right: 120px; bottom: 0; padding: 12px;