Skip to content

Commit

Permalink
Rewrite web component
Browse files Browse the repository at this point in the history
  • Loading branch information
warriordog committed Feb 17, 2019
1 parent 6caca5e commit 2ad47b2
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 213 deletions.
13 changes: 8 additions & 5 deletions src/main/java/net/acomputerdog/webchat/net/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.sun.net.httpserver.HttpServer;
import net.acomputerdog.webchat.PluginWebChat;
import net.acomputerdog.webchat.net.handler.*;
import net.acomputerdog.webchat.net.handler.ChatUpdateHandler;
import net.acomputerdog.webchat.net.handler.ChatVersionHandler;
import net.acomputerdog.webchat.net.handler.SendHandler;
import net.acomputerdog.webchat.net.handler.SimpleHandler;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -19,7 +21,7 @@ public class WebServer {
private final Logger logger;
private final PluginWebChat plugin;

public WebServer(PluginWebChat plugin) throws IOException, URISyntaxException {
public WebServer(PluginWebChat plugin) throws IOException {
this.plugin = plugin;
this.logger = plugin.getLogger();
//set timeouts. Is either 10 or 100 seconds (unsure do to closed source/undocumented code)
Expand All @@ -36,10 +38,11 @@ public WebServer(PluginWebChat plugin) throws IOException, URISyntaxException {
}
});
serverThread.setName("web_server");
SimpleHandler main = new SimpleHandler(this, "/main.html");
SimpleHandler main = new SimpleHandler(this, "/web/main.html");
server.createContext("/", main);
server.createContext("/main.html", main);
server.createContext("/chat", new ChatHandler(this, plugin));
server.createContext("/js/chatClient.js", new SimpleHandler(this, "/web/js/chatClient.js"));
server.createContext("/css/main", new SimpleHandler(this, "/web/css/main.css"));
server.createContext("/updatechat", new ChatUpdateHandler(this, plugin));
server.createContext("/chatversion", new ChatVersionHandler(this, plugin));
server.createContext("/send", new SendHandler(this, logger, plugin));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public void handleExchange(HttpExchange exchange) throws IOException {
sendResponse(exchange, "<p>405 Method not allowed: only POST is accepted.</p>", 405);
return;
}
if (!"application/x-www-form-urlencoded".equals(exchange.getRequestHeaders().get("Content-Type").get(0))) {
sendResponse(exchange, "<p>406 Not acceptable: wrong Content-Type.</p>", 406);
return;
}
if (!checkAndUpdateTimeout(exchange.getRemoteAddress())) {
sendResponse(exchange, "<p>429 Too many requests: please wait to send multiple messages.</p>", 429);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.stream.Collectors;

/**
* API endpoint that returns a hard-coded string reponse
* API endpoint that returns a hard-coded string response
*/
public class SimpleHandler extends WebHandler {
private final String page;
Expand Down
161 changes: 0 additions & 161 deletions src/main/resources/main.html

This file was deleted.

42 changes: 42 additions & 0 deletions src/main/resources/web/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
body {
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: stretch;

padding: 10px;
margin: 0;

position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}

#sendContainer {
display: flex;
flex-direction: row;
justify-content: stretch;
align-items: flex-start;

padding: 0;
margin: 10px 0 0;

}

#sendText {
flex-grow: 1;
flex-shrink: 1;
}

#sendButton {
margin-left: 10px;
}

#chatbox {
flex-grow: 1;
flex-shrink: 1;

resize: none;
}
81 changes: 81 additions & 0 deletions src/main/resources/web/js/chatClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const DEFAULT_TIMEOUT = 500;
const MAX_TIMEOUT = 5000;
const TIMEOUT_INCREASE = 100;

let chatTimer;
let version = 0;
let timeout = DEFAULT_TIMEOUT;

function sendChat() {
let sendBox = document.getElementById("sendText");
if (sendBox) {
let http = new XMLHttpRequest();
http.open("POST", "send", true);
http.send("message=" + sendBox.value);

sendBox.value = "";

clearTimeout(chatTimer);
timeout = DEFAULT_TIMEOUT;
setTimeout(refreshChat, timeout);
}
}

function updateChat(response) {
let chatbox = document.getElementById("chatbox");
if (chatbox) {
chatbox.value = response;
}
}

function refreshChat() {
let http = new XMLHttpRequest();
http.onreadystatechange = function () {
if (http.readyState === 4) {
if (http.status === 200) {
timeout = DEFAULT_TIMEOUT;
updateChat(http.responseText);
refreshVersion();
} else if (timeout < MAX_TIMEOUT) {
timeout += TIMEOUT_INCREASE;
}
//set timeout for next chat query
chatTimer = window.setTimeout(refreshChat, timeout);
}
};
http.open("GET", "updatechat" + "?version=" + version, true); // true for asynchronous
http.send();
}

function refreshVersion() {
let http = new XMLHttpRequest();
http.onreadystatechange = function () {
if (http.readyState === 4 && http.status === 200) {
version = http.responseText;
}
};
http.open("GET", "chatversion", true);
http.send();
}

function onPost() {
setTimeout(function () {
let messageBox = document.getElementById("message");
if (messageBox) {
messageBox.value = "";
}
}, 100);
clearTimeout(chatTimer);
timeout = DEFAULT_TIMEOUT;
setTimeout(refreshChat, 100);
return true;
}

function onSendKey(key) {
if (key === 13) {
sendChat()
}
}

//start everything going
refreshChat();
Loading

0 comments on commit 2ad47b2

Please sign in to comment.