Skip to content

Commit

Permalink
Chat history
Browse files Browse the repository at this point in the history
  • Loading branch information
herronjo committed Nov 16, 2024
1 parent 8b0ce45 commit add1767
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tv/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@ window.addEventListener("load", function() {
lastUserToSend = data.user.username;
});

socket.on("history", function(data) {
lastUserToSend = "";
data.forEach(function(msg) {
switch (msg.event) {
case "message": {
const msgElem = new ChatMessageComponent(msg.user, msg.message, lastUserToSend !== msg.user.username);
msgElem.classList.add("width100");
msgElem.setAttribute("title", new Date(msg.time).toLocaleString());
$("#chatmessages").append(msgElem);
lastUserToSend = msg.user.username;
break;
}
case "join": {
const evt = document.createElement("i");
evt.classList.add("width100", "sysnotif");
evt.setAttribute("title", new Date(msg.time).toLocaleString());
evt.textContent = `${msg.username} joined the chat.`;
$("#chatmessages").append(evt);
break;
}
case "leave": {
const evt = document.createElement("i");
evt.classList.add("width100", "sysnotif");
evt.setAttribute("title", new Date(msg.time).toLocaleString());
evt.textContent = `${msg.username} left the chat.`;
$("#chatmessages").append(evt);
break;
}
}
});
$("#chatmessages").scrollTop = $("#chatmessages").scrollHeight;
});

socket.on("typing", function(data) {
if (data.typing.length === 0) {
$("#typing").textContent = "";
Expand Down
33 changes: 33 additions & 0 deletions tv/tv.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,39 @@ window.addEventListener("load", function() {
lastUserToSend = data.user.username;
});

socket.on("history", function(data) {
lastUserToSend = "";
data.forEach(function(msg) {
switch (msg.event) {
case "message": {
const msgElem = new ChatMessageComponent(msg.user, msg.message, lastUserToSend !== msg.user.username);
msgElem.classList.add("width100");
msgElem.setAttribute("title", new Date(msg.time).toLocaleString());
$("#chatmessages").append(msgElem);
lastUserToSend = msg.user.username;
break;
}
case "join": {
const evt = document.createElement("i");
evt.classList.add("width100", "sysnotif");
evt.setAttribute("title", new Date(msg.time).toLocaleString());
evt.textContent = `${msg.username} joined the chat.`;
$("#chatmessages").append(evt);
break;
}
case "leave": {
const evt = document.createElement("i");
evt.classList.add("width100", "sysnotif");
evt.setAttribute("title", new Date(msg.time).toLocaleString());
evt.textContent = `${msg.username} left the chat.`;
$("#chatmessages").append(evt);
break;
}
}
});
$("#chatmessages").scrollTop = $("#chatmessages").scrollHeight;
});

socket.on("typing", function(data) {
// Remove self from typing list
const index = data.typing.indexOf(api.username);
Expand Down

0 comments on commit add1767

Please sign in to comment.