|
|
@ -55,10 +55,10 @@ ws.onmessage = function(info) {
|
|
|
|
function newMessage(data) {
|
|
|
|
function newMessage(data) {
|
|
|
|
switch(data.type) {
|
|
|
|
switch(data.type) {
|
|
|
|
case "join":
|
|
|
|
case "join":
|
|
|
|
userJoin(data.content);
|
|
|
|
userJoin(data.content, data.date);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case "leave":
|
|
|
|
case "leave":
|
|
|
|
userLeave(data.content);
|
|
|
|
userLeave(data.content, data.date);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case "message":
|
|
|
|
case "message":
|
|
|
|
chatMessage(data.sender, data.content, data.date)
|
|
|
|
chatMessage(data.sender, data.content, data.date)
|
|
|
@ -79,29 +79,50 @@ function chatMessage(sender, content, date) {
|
|
|
|
text.innerText = content;
|
|
|
|
text.innerText = content;
|
|
|
|
|
|
|
|
|
|
|
|
msg.append(hr, author, timestamp, text);
|
|
|
|
msg.append(hr, author, timestamp, text);
|
|
|
|
|
|
|
|
insertMessage(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function serverMessage(content, date) {
|
|
|
|
|
|
|
|
let msg = create("div", "message");
|
|
|
|
|
|
|
|
let hr = create("hr", "seperator");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let timestamp = create("div", "timestamp");
|
|
|
|
|
|
|
|
timestamp.innerText = dateToString(new Date(date));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let text = create("div", "server-message");
|
|
|
|
|
|
|
|
text.innerText = content;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msg.append(hr, timestamp, text);
|
|
|
|
|
|
|
|
insertMessage(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function insertMessage(div) {
|
|
|
|
//If message scrolling at the bottom, move to bottom again to update
|
|
|
|
//If message scrolling at the bottom, move to bottom again to update
|
|
|
|
let scroller = document.getElementsByClassName("messages")[0];
|
|
|
|
let scroller = document.getElementsByClassName("messages")[0];
|
|
|
|
let currentScroll = scroller.scrollTop;
|
|
|
|
let currentScroll = scroller.scrollTop;
|
|
|
|
let maxScroll = scroller.scrollHeight - scroller.clientHeight;
|
|
|
|
let maxScroll = scroller.scrollHeight - scroller.clientHeight;
|
|
|
|
|
|
|
|
|
|
|
|
document.getElementById("messages").append(msg);
|
|
|
|
document.getElementById("messages").append(div);
|
|
|
|
|
|
|
|
|
|
|
|
if (currentScroll === maxScroll) {
|
|
|
|
if (currentScroll === maxScroll) {
|
|
|
|
msg.scrollIntoView();
|
|
|
|
div.scrollIntoView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
function userJoin(user) {
|
|
|
|
* Join/leave events
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function userJoin(user, date) {
|
|
|
|
let p = document.createElement("p");
|
|
|
|
let p = document.createElement("p");
|
|
|
|
p.className = user;
|
|
|
|
p.className = user;
|
|
|
|
p.innerText = user;
|
|
|
|
p.innerText = user;
|
|
|
|
document.getElementById("users").appendChild(p);
|
|
|
|
document.getElementById("users").appendChild(p);
|
|
|
|
|
|
|
|
serverMessage(user + " has joined the server", date);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function userLeave(user) {
|
|
|
|
function userLeave(user, date) {
|
|
|
|
users = document.getElementById("users");
|
|
|
|
users = document.getElementById("users");
|
|
|
|
users.removeChild(users.getElementsByClassName(user)[0]);
|
|
|
|
users.removeChild(users.getElementsByClassName(user)[0]);
|
|
|
|
|
|
|
|
serverMessage(user + " has left the server", date);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|