slight cleanup, remove thread sleep, low risk of spinning
This commit is contained in:
@@ -4,20 +4,14 @@ import org.lumijiez.core.http.HttpServer;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
HttpServer httpServer = new HttpServer(8080);
|
||||
HttpServer server = new HttpServer(8080);
|
||||
|
||||
httpServer.GET("/hello", (req, res) -> {
|
||||
res.sendResponse(200, "Hello, World!");
|
||||
});
|
||||
server.GET("/hello", (req, res) -> res.sendResponse(200, "Hello, World!"));
|
||||
|
||||
httpServer.GET("/goodbye", (req, res) -> {
|
||||
res.sendResponse(200, "Goodbye, World!");
|
||||
});
|
||||
server.GET("/goodbye", (req, res) -> res.sendResponse(200, "Goodbye, World!"));
|
||||
|
||||
httpServer.POST("/data", (req, res) -> {
|
||||
res.sendResponse(200, "Data received");
|
||||
});
|
||||
server.POST("/data", (req, res) -> res.sendResponse(200, "Data received"));
|
||||
|
||||
httpServer.start();
|
||||
server.start();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import org.lumijiez.logging.Logger;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class HttpResponse {
|
||||
private final BufferedWriter out;
|
||||
@@ -20,7 +21,7 @@ public class HttpResponse {
|
||||
|
||||
out.write("Content-Type: text/plain");
|
||||
out.write("\r\n");
|
||||
out.write("Content-Length: " + message.getBytes("UTF-8").length);
|
||||
out.write("Content-Length: " + message.getBytes(StandardCharsets.UTF_8).length);
|
||||
out.write("\r\n");
|
||||
out.write("Connection: keep-alive");
|
||||
out.write("\r\n");
|
||||
|
||||
@@ -82,7 +82,7 @@ public class HttpServer {
|
||||
while (keepAlive && requestCount < MAX_REQUESTS_PER_CONNECTION && running) {
|
||||
try {
|
||||
if (!in.ready()) {
|
||||
Thread.sleep(10);
|
||||
// Thread.sleep(10);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -111,9 +111,6 @@ public class HttpServer {
|
||||
} catch (SocketTimeoutException e) {
|
||||
Logger.info("HTTP", "Keep-alive timeout reached");
|
||||
break;
|
||||
} catch (InterruptedException e) {
|
||||
Logger.info("HTTP", "Connection handling interrupted");
|
||||
break;
|
||||
} catch (IOException e) {
|
||||
if (running) {
|
||||
Logger.error("HTTP", "Error processing request: " + e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user