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 class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
HttpServer httpServer = new HttpServer(8080);
|
HttpServer server = new HttpServer(8080);
|
||||||
|
|
||||||
httpServer.GET("/hello", (req, res) -> {
|
server.GET("/hello", (req, res) -> res.sendResponse(200, "Hello, World!"));
|
||||||
res.sendResponse(200, "Hello, World!");
|
|
||||||
});
|
|
||||||
|
|
||||||
httpServer.GET("/goodbye", (req, res) -> {
|
server.GET("/goodbye", (req, res) -> res.sendResponse(200, "Goodbye, World!"));
|
||||||
res.sendResponse(200, "Goodbye, World!");
|
|
||||||
});
|
|
||||||
|
|
||||||
httpServer.POST("/data", (req, res) -> {
|
server.POST("/data", (req, res) -> res.sendResponse(200, "Data received"));
|
||||||
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.BufferedWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class HttpResponse {
|
public class HttpResponse {
|
||||||
private final BufferedWriter out;
|
private final BufferedWriter out;
|
||||||
@@ -20,7 +21,7 @@ public class HttpResponse {
|
|||||||
|
|
||||||
out.write("Content-Type: text/plain");
|
out.write("Content-Type: text/plain");
|
||||||
out.write("\r\n");
|
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("\r\n");
|
||||||
out.write("Connection: keep-alive");
|
out.write("Connection: keep-alive");
|
||||||
out.write("\r\n");
|
out.write("\r\n");
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ public class HttpServer {
|
|||||||
while (keepAlive && requestCount < MAX_REQUESTS_PER_CONNECTION && running) {
|
while (keepAlive && requestCount < MAX_REQUESTS_PER_CONNECTION && running) {
|
||||||
try {
|
try {
|
||||||
if (!in.ready()) {
|
if (!in.ready()) {
|
||||||
Thread.sleep(10);
|
// Thread.sleep(10);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,9 +111,6 @@ public class HttpServer {
|
|||||||
} catch (SocketTimeoutException e) {
|
} catch (SocketTimeoutException e) {
|
||||||
Logger.info("HTTP", "Keep-alive timeout reached");
|
Logger.info("HTTP", "Keep-alive timeout reached");
|
||||||
break;
|
break;
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Logger.info("HTTP", "Connection handling interrupted");
|
|
||||||
break;
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (running) {
|
if (running) {
|
||||||
Logger.error("HTTP", "Error processing request: " + e.getMessage());
|
Logger.error("HTTP", "Error processing request: " + e.getMessage());
|
||||||
|
|||||||
Reference in New Issue
Block a user