Skip to content

Commit

Permalink
solved uni-directional message bug, starting user listing
Browse files Browse the repository at this point in the history
  • Loading branch information
toddync committed Oct 13, 2023
1 parent 3e8bbf0 commit 264686a
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
nodeSocket/db.js
28 changes: 15 additions & 13 deletions javascript/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ conn.addEventListener('error', function(event) {
$("#error").html("There was an error in your conection to the server, or the devs forget to turn the server on...");
});

document.getElementById("id").innerHTML = "Your id is: " + sessionStorage.getItem("id") || 1;
document.getElementById("user").setAttribute("data-User-Id", sessionStorage.getItem("id")) || 1;
document.getElementById("user").setAttribute("src", sessionStorage.getItem("img"));
document.getElementById("id").innerHTML = "Your id is: " + (sessionStorage.getItem("id") || 1);
document.getElementById("user").setAttribute("data-User-Id", (sessionStorage.getItem("id")) || 1);
document.getElementById("user").setAttribute("src", (sessionStorage.getItem("img") || "./images/default.png"));

/* VANILLA PART */

Expand Down Expand Up @@ -178,17 +178,18 @@ conn.onopen = () => {
}

conn.onmessage = function(e) {
data = JSON.parse(e.data)
console.log(data);
//console.log(e.data);
if (data.status === "success") {
var data = JSON.parse(e.data)
//console.log(data);
console.log(data.status);
console.log(JSON.stringify(data, null, 2))
if (data.status == "success") {

if (data.ctx === "loadChat") {

data.msgs.forEach(msg => {
console.log(msg, "\n")

document.getElementById("result").innerHTML += buildMsg(msg, data.reqId);
document.getElementById("result").innerHTML += buildMsg(msg, data.reqId, data.chat);

})

Expand Down Expand Up @@ -464,14 +465,15 @@ function leave() {
}
}

function buildMsg(msg, id) {
function buildMsg(msg, id, chat) {
var date = new Date();
msg.date = new Date(msg.date);

if(date == msg.date){
/* if(date == msg.date){
if(date.getFullYear() == msg.date.getFullYear){}
}

*/

if (msg.sender == id) {

var r =`
Expand Down Expand Up @@ -500,7 +502,7 @@ function buildMsg(msg, id) {
</div>
<div class='chat-msg-content'>
<span>${msg.username}</span>
<span>${chat}</span>
<div id='${msg.id}' class='chat-msg-text'>${msg.message.replace("\n", "<br>")}</div>
</div>
</div>
Expand All @@ -511,7 +513,7 @@ function buildMsg(msg, id) {
}


return " ";
return "o";
}


Expand Down
77 changes: 58 additions & 19 deletions nodeSocket/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const dump = require('var_dump');
const url = require("node:url")

const message = require("./message.js");
const user = require("./user.js");

var mysql = require('./db.js');
mysql = mysql.getSql();
Expand All @@ -15,58 +16,96 @@ async function main() {

u[0].forEach(user => {
users[user.id] = user;
dump(user.id)
})


const Server = new Ws.Server({
port: 4000
})

Server.on("connection", (ws, req) => {
Server.on("connection", async (ws, req) => {

var query = url.parse(req.url, true).query;
ws.userId = query.id;
ws.userId = url.parse(req.url, true).query.id;
users[ws.userId].Status = 'online';

mysql.query(`UPDATE chat SET Status='online' WHERE id='${query.id}'`);
mysql.query(`UPDATE chat SET Status='online' WHERE id='${ws.userId}'`);

Loadusers(ws)

ws.on("message", (msg, bin) => {
ws.on("message", msg => {
msg = JSON.parse(msg.toString('utf8'));

if (msg.act === "request") {
requestHandler(msg, ws, users[msg.chat].Name);
switch (msg.act) {

case "request":
requestHandler(msg, ws, users[msg.chat].Name);
break;

}

});

ws.on("close",
ws => {

//mysql.query(`UPDATE chat SET Status='offline' WHERE id='${query.id}'`);
ws.on("close", ws => {

//mysql.query(`UPDATE chat SET Status='offline' WHERE id='${query.id}'`);

//users[ws.userId].Status = 'offline';

//users[ws.userId].Status = 'offline';
})

})

});
}

main();



async function requestHandler(req, ws, chat) {

var response;

var ctx = req.ctx;

switch (ctx) {

case "msg":

if (req.ctx === "msg") {
response = await message.getMessageArray(ws.userId, req.chat);

ws.send(JSON.stringify({
act: "response",
ctx: "loadChat",
status: "success",
reqId: ws.userId,
chat: chat,
msgs: response
}))

response = await message.getMessageArray(ws.userId, req.chat);
break;

ws.send(JSON.stringify({
act: "response", ctx: "loadChat", status: "success", reqId: ws.userId, username: chat, msgs: response
}))
case "user":

response = await user.getUserArray(ws.userId);

ws.send({
act:"response",
ctx:""
});

break;
}

}

async function Loadusers(ws){

var u = user.getUserArray(ws.userId);

ws.send(JSON.stringify({
act: "firstLoad",
ctx: "user",
status: "success",
users: u
}));
}
12 changes: 8 additions & 4 deletions nodeSocket/message.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
var mysql = require('./db.js');
mysql = mysql.getSql();

//$query = "SELECT * FROM (SELECT * FROM mensagens WHERE message LIKE '%".$search."%' AND sender = '".$chat."' AND reciever = '".$user."' OR message LIKE '%".$search."%' AND sender = '".$user."' AND reciever = '".$chat."' ORDER BY msg_id DESC LIMIT 50)Var1 ORDER BY msg_id ASC ";

async function getMessageArray(from, to){

var result = await mysql.query(`SELECT * FROM (SELECT * FROM mensagens WHERE sender=${from} AND reciever=${to} ORDER BY msg_id DESC LIMIT 50)Var1 ORDER BY msg_id ASC`);
var result = await mysql.query(`
SELECT * FROM
(SELECT * FROM mensagens WHERE
(sender=${from} AND reciever=${to} OR sender=${to} AND reciever=${from})
ORDER BY msg_id DESC LIMIT 50)Var1
ORDER BY date ASC`);

mysql.query(`UPDATE mensagens SET seen='1' WHERE sender=${from} AND reciever=${to}`);

return result[0];
return result[0]

}

module.exports = {getMessageArray: getMessageArray}
module.exports = {getMessageArray: getMessageArray}
30 changes: 30 additions & 0 deletions nodeSocket/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var mysql = require('./db.js');
mysql = mysql.getSql();

async function getUserArray(id) {

var r = await mysql.query(`
SELECT chat.*
FROM chat
INNER JOIN users_relation ON chat.id = users_relation.usr2
WHERE users_relation.usr1 = ${id};`);

return r[0];

console.log("\n\n")

var r = await mysql.query(`
SELECT DISTINCT chat.*
FROM chat
INNER JOIN users_relation ON chat.id = users_relation.usr2
INNER JOIN mensagens ON chat.id = mensagens.sender OR chat.id = mensagens.reciever
WHERE users_relation.usr1 = ${id}
ORDER BY mensagens.date;`);

console.log(JSON.stringify(r[0], null, 2));

}

module.exports = {
getUserArray: getUserArray
};

0 comments on commit 264686a

Please sign in to comment.