2019-09-25 09:03:29 +07:00
|
|
|
//Saved nickname from previous usage
|
|
|
|
let nickname = window.localStorage.getItem("nickname");
|
|
|
|
|
|
|
|
//Nickname button and input
|
|
|
|
let nickI = document.getElementById("nickI");
|
|
|
|
let nickB = document.getElementById("nickB");
|
|
|
|
|
|
|
|
//Chatting button and input
|
|
|
|
let chatI = document.getElementById("chatI");
|
|
|
|
let chatB = document.getElementById("chatB");
|
|
|
|
|
|
|
|
let registered = false;
|
|
|
|
let modal = document.getElementById("modal");
|
|
|
|
|
|
|
|
//Gets websocket url by replacing http with ws and https with wss, and appending /websocket to the host
|
|
|
|
let protocol = location.protocol == "http:" ? "ws:" : "wss:";
|
|
|
|
let url = protocol + "//" + location.host + "/websocket";
|
|
|
|
|
2020-09-02 03:37:23 +07:00
|
|
|
if (nickname != null) {
|
|
|
|
nickI.value = nickname;
|
|
|
|
}
|
|
|
|
|
2019-09-25 09:03:29 +07:00
|
|
|
let ws = new WebSocket(url);
|
|
|
|
|
|
|
|
ws.onopen = function() {
|
|
|
|
modal.style.display = "block";
|
|
|
|
nickI.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.onmessage = function(info) {
|
|
|
|
let data = JSON.parse(info.data);
|
|
|
|
if(!data.success) {
|
|
|
|
toast("Error: " + data.content);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!registered) {
|
|
|
|
registered = true;
|
|
|
|
window.localStorage.setItem("nickname", nickname);
|
|
|
|
modal.style.display = "none";
|
|
|
|
chatI.focus()
|
|
|
|
let users = data.content;
|
|
|
|
for(let i = 0; i < users.length; i++) {
|
|
|
|
userJoin(users[i]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newMessage(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Message receive handling
|
|
|
|
*/
|
|
|
|
|
|
|
|
function newMessage(data) {
|
|
|
|
switch(data.type) {
|
|
|
|
case "join":
|
2019-09-26 00:44:45 +07:00
|
|
|
userJoin(data.content, data.date);
|
2019-09-26 01:11:28 +07:00
|
|
|
serverMessage(data.content + " has joined the server", data.date);
|
2019-09-25 09:03:29 +07:00
|
|
|
break;
|
|
|
|
case "leave":
|
2019-09-26 00:44:45 +07:00
|
|
|
userLeave(data.content, data.date);
|
2019-09-25 09:03:29 +07:00
|
|
|
break;
|
|
|
|
case "message":
|
|
|
|
chatMessage(data.sender, data.content, data.date)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function chatMessage(sender, content, date) {
|
|
|
|
let msg = create("div", "message");
|
|
|
|
|
2020-09-02 03:37:23 +07:00
|
|
|
aClasses = "author"
|
|
|
|
if (sender === nickname) {
|
|
|
|
aClasses += " self"
|
|
|
|
}
|
|
|
|
let author = create("div", aClasses);
|
2019-09-25 09:03:29 +07:00
|
|
|
author.innerText = sender;
|
|
|
|
|
|
|
|
let timestamp = create("div", "timestamp");
|
|
|
|
timestamp.innerText = dateToString(new Date(date));
|
|
|
|
|
|
|
|
let text = create("div", "content");
|
|
|
|
text.innerText = content;
|
2019-09-26 00:44:45 +07:00
|
|
|
|
2020-09-02 03:37:23 +07:00
|
|
|
msg.append(author, timestamp, text);
|
2019-09-26 00:44:45 +07:00
|
|
|
insertMessage(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function serverMessage(content, date) {
|
|
|
|
let msg = create("div", "message");
|
|
|
|
|
|
|
|
let text = create("div", "server-message");
|
|
|
|
text.innerText = content;
|
2019-09-25 09:03:29 +07:00
|
|
|
|
2020-09-02 03:37:23 +07:00
|
|
|
msg.append(text);
|
2019-09-26 00:44:45 +07:00
|
|
|
insertMessage(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertMessage(div) {
|
2019-09-25 09:03:29 +07:00
|
|
|
//If message scrolling at the bottom, move to bottom again to update
|
|
|
|
let scroller = document.getElementsByClassName("messages")[0];
|
|
|
|
let currentScroll = scroller.scrollTop;
|
|
|
|
let maxScroll = scroller.scrollHeight - scroller.clientHeight;
|
|
|
|
|
2019-09-26 00:44:45 +07:00
|
|
|
document.getElementById("messages").append(div);
|
2019-09-25 09:03:29 +07:00
|
|
|
|
|
|
|
if (currentScroll === maxScroll) {
|
2019-09-26 00:44:45 +07:00
|
|
|
div.scrollIntoView();
|
2019-09-25 09:03:29 +07:00
|
|
|
}
|
|
|
|
}
|
2019-09-26 00:44:45 +07:00
|
|
|
/**
|
|
|
|
* Join/leave events
|
|
|
|
*/
|
|
|
|
function userJoin(user, date) {
|
2019-09-25 09:03:29 +07:00
|
|
|
let p = document.createElement("p");
|
|
|
|
p.className = user;
|
|
|
|
p.innerText = user;
|
|
|
|
document.getElementById("users").appendChild(p);
|
|
|
|
}
|
|
|
|
|
2019-09-26 00:44:45 +07:00
|
|
|
function userLeave(user, date) {
|
2019-09-25 09:03:29 +07:00
|
|
|
users = document.getElementById("users");
|
|
|
|
users.removeChild(users.getElementsByClassName(user)[0]);
|
2019-09-26 00:44:45 +07:00
|
|
|
serverMessage(user + " has left the server", date);
|
2019-09-25 09:03:29 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Registration
|
|
|
|
*/
|
|
|
|
function register() {
|
|
|
|
let nick = nickI.value;
|
|
|
|
if (nick.trim().length == 0) return;
|
|
|
|
nickname = nick;
|
|
|
|
ws.send(nick);
|
|
|
|
}
|
|
|
|
|
|
|
|
nickI.onkeydown = function(ev) {
|
|
|
|
if(ev.key !== "Enter") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
register();
|
|
|
|
}
|
|
|
|
|
|
|
|
nickB.onclick = function() {
|
|
|
|
register();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Message sending
|
|
|
|
*/
|
|
|
|
|
|
|
|
chatI.onkeydown = function(ev) {
|
|
|
|
if(ev.key !== "Enter") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(chatI.value.trim().length == 0) return;
|
|
|
|
|
|
|
|
ws.send(chatI.value);
|
|
|
|
chatI.value = "";
|
|
|
|
}
|
2019-09-25 13:23:48 +07:00
|
|
|
chatB.onclick = function() {
|
2019-09-25 09:03:29 +07:00
|
|
|
if(chatI.value.trim().length == 0) return;
|
|
|
|
|
|
|
|
ws.send(chatI.value);
|
|
|
|
chatI.value = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Util functions
|
|
|
|
*/
|
|
|
|
function toast(message) {
|
|
|
|
var x = document.getElementById("snackbar");
|
|
|
|
x.innerText = message;
|
|
|
|
x.className = "snackshow";
|
|
|
|
setTimeout(function(){ x.className = x.className.replace("snackshow", ""); }, 3000);
|
|
|
|
}
|
|
|
|
|
|
|
|
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
|
|
|
|
function dateToString(date) {
|
|
|
|
let year = date.getFullYear();
|
|
|
|
let month = months[date.getMonth()];
|
|
|
|
let day = date.getDate();
|
|
|
|
let hour = date.getHours();
|
|
|
|
let minute = date.getMinutes();
|
|
|
|
return ` ${day} ${month} ${year} at ${hour}:${minute}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function create(name, classes) {
|
|
|
|
let x = document.createElement(name);
|
|
|
|
x.className = classes;
|
|
|
|
return x;
|
|
|
|
}
|