Fixes and improvements

- Fixed message receiving
- Fixed message chunk loading
- Added unread indicator
- Other fixes
This commit is contained in:
Norbi Peti 2016-08-16 12:51:06 +02:00
parent 01a1ce097c
commit 4b90fdf8b9
9 changed files with 71 additions and 38 deletions

View file

@ -1,6 +1,6 @@
<!DOCTYPE html>
<head>
<title>ChatServer</title>
<title>Chat</title>
<meta charset="UTF-8"/>
<script src="js/jquery-3.1.0.js"></script>
<script src="js/moment-with-locales.js" charset="UTF-8"></script>

View file

@ -8,7 +8,8 @@ $(document).ready(function () {
var nodes = cmsgs.children;
for (var x = 0; x < nodes.length; x++) {
var item = nodes[x];
handlereceivedmessage(item);
if (item.tagName == "DIV")
handlereceivedmessage(item);
}
}
});

View file

@ -1,4 +1,4 @@
var handlereceivedmessage = function handlereceivedmessage(msgnode) {
function handlereceivedmessage(msgnode) {
var spans = msgnode.getElementsByTagName("span");
var ctime = null;
for (var i = 0; i < spans.length; i++)
@ -9,22 +9,53 @@ var handlereceivedmessage = function handlereceivedmessage(msgnode) {
msgnode.scrollIntoView(false);
}
(function poll() {
setTimeout(function () {
$.ajax({
url: "/receivemessage", success: function (data) {
console.log(data);
var msgelement = document.getElementById("channelmessages").appendChild(document.createElement("div"));
var header = msgelement.appendChild(document.createElement("p");
header.innerText = data.sender.name + " - ";
var isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
header.appendElement("span").addClass("converttime")
.value = isoFormat.format(data.time) + "+00:00";
var body = msgelement.appendChild(document.createElement("p"));
body.innerText = data.message;
handlereceivedmessage(msgnode);
}, dataType: "json", complete: poll
});
}, 100);
})();
var unreadCount = 0;
var updateUnreadCount = function () {
if (unreadCount > 0)
document.title = "(" + unreadCount + ") Chat";
else
document.title = "Chat";
var msgs = document.getElementById("channelmessages").children;
for (var i = msgs.length - 1; i >= 0; i--) {
if (i >= msgs.length - unreadCount)
msgs[i].style.backgroundColor = "darkgray";
else
msgs[i].style = "";
}
};
var addUnread = function addUnread() {
unreadCount++;
updateUnreadCount();
};
var resetUnread = function resetUnread() {
unreadCount = 0;
updateUnreadCount();
};
(function poll() {
setTimeout(function () {
$.ajax({
url: "/receivemessage", success: function (data) {
console.log(data);
var msgelement = document.getElementById("channelmessages").appendChild(document.createElement("div"));
var header = msgelement.appendChild(document.createElement("p"));
header.innerText = data.sender.name + " - ";
var span = header.appendChild(document.createElement("span"));
span.className = "converttime";
span.innerText = data.time;
var body = msgelement.appendChild(document.createElement("p"));
body.innerText = data.message;
handlereceivedmessage(msgelement);
addUnread();
}, dataType: "json", complete: poll
});
}, 100);
})();
$(document).ready(function () {
$('#msginput').on("focus", resetUnread);
$('#msginput').on("keydown", resetUnread);
});

View file

@ -17,11 +17,13 @@ var respfunc = function respfunc(result) {
else {
showError(result.responseText);
msginput.disabled = false;
msginput.focus();
}
}
else {
msginput.value = "";
msginput.disabled = false;
msginput.focus();
}
};
@ -31,7 +33,7 @@ var sendmsgonenter = function sendmsgonenter(e) {
return;
}
e.preventDefault();
var textarea = event.target;
var textarea = e.target;
if (textarea.value.trim().length == 0)
return;
textarea.disabled = true; //msginput

View file

@ -17,4 +17,4 @@ function showError(message) {
errormsg.innerHTML = message;
errormsg.style = "display: block";
setTimeout(function () { errormsg.style.display = "none"; }, 2000);
}
}

View file

@ -58,14 +58,9 @@ public class IndexPage extends Page {
cide.attr("id", "convidp");
cide.text(Long.toString(conv.getId()));
LogManager.getLogger().log(Level.DEBUG, "Messages: " + conv.getMesssageChunks().size());
@SuppressWarnings("unchecked")
LoaderCollection<MessageChunk> chunks = (LoaderCollection<MessageChunk>) conv.getMesssageChunks()
.clone();
Collections.reverse(chunks);
for (MessageChunk chunk : chunks) {
for (Message message : chunk.getMessages()) {
message.getAsHTML(channelmessages);
}
MessageChunk chunk = conv.getMesssageChunks().get(conv.getMesssageChunks().size() - 1);
for (Message message : chunk.getMessages()) {
message.getAsHTML(channelmessages);
}
return doc;
}, exchange);

View file

@ -49,10 +49,14 @@ public class ReceiveMessageAjaxPage extends Page {
public static void sendMessageBack(Message msg, Conversation conv) throws IOException {
for (User user : conv.getUsers()) {
LogManager.getLogger().debug("User: " + user);
if (exmap.containsKey(user)) {
if (exmap.containsKey(user)) { // TODO: Save new messages if not listening
LogManager.getLogger().debug("Exmap contains user");
JsonObject msgobj = msg.getAsJson();
IOHelper.SendResponse(200, msgobj.toString(), exmap.get(user));
try {
IOHelper.SendResponse(200, msgobj.toString(), exmap.get(user));
} catch (IOException e) { // Remove users even if an error occurs (otherwise they may not be able to send a new message due to "headers already sent")
e.printStackTrace();
}
exmap.remove(user);
} else
LogManager.getLogger().warn("User is not listening: " + user);

View file

@ -23,12 +23,12 @@ public class RegisterAjaxPage extends Page {
return; // TODO: Use JavaScript too, for error checks
}
for (User user : DataManager.getAll(User.class)) { // TODO: Optimize
if (post.get("email").equals(user.getEmail())) {
errormsg += "<p>An user with this name already exists</p>";
if (post.get("email").getAsString().equals(user.getEmail())) {
errormsg += "<p>An user with this E-mail already exists</p>";
break;
}
}
if (!post.get("pass").equals(post.get("pass2")))
if (!post.get("pass").getAsString().equals(post.get("pass2").getAsString()))
errormsg += "<p>The passwords don't match</p>";
if (errormsg.length() > 0) {
final String msg = errormsg;

View file

@ -69,7 +69,7 @@ public class SendMessageAjaxPage extends Page {
msg.setTime(new Date());
DataManager.save(conv);
LogManager.getLogger().log(Level.DEBUG,
"Added conversation's message count: " + conv.getMesssageChunks().size());
"Added conversation's messagechunk count: " + conv.getMesssageChunks().size());
ReceiveMessageAjaxPage.sendMessageBack(msg, conv);