Repository navigation
Expand file tree
/
Copy pathWebSOcket.java
More file actions
58 lines (52 loc) · 1.96 KB
/
Copy pathWebSOcket.java
File metadata and controls
58 lines (52 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Minecraft.Host;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
public class WebSOcket {
private final int port;
private final Host host;
private final Game game;
private HttpServer server;
public WebSOcket(int port, Host host, Game game) {
this.port = port;
this.host = host;
this.game = game;
}
public void start() {
try {
server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/status", new StatusHandler());
server.setExecutor(null);
server.start();
System.out.println("[WebAPI] Webステータスサーバーを起動しました (Port: " + port + ")");
} catch (IOException e) {
System.err.println("[WebAPI] Webステータスサーバーの起動に失敗: " + e.getMessage());
}
}
public void stop() {
if (server != null) {
server.stop(0);
}
}
private class StatusHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
String jsonResponse = String.format(
"{\"running\":%b, \"ping\":%d, \"mc_port\":%d, \"ip\":\"%s\"}",
game.isRunning(),
host.ping(),
host.getPort().getPortNumber(),
host.getIpAddress().getAddress()
);
exchange.getResponseHeaders().set("Content-Type", "application/json; charset=UTF-8");
byte[] bytes = jsonResponse.getBytes("UTF-8");
exchange.sendResponseHeaders(200, bytes.length);
OutputStream os = exchange.getResponseBody();
os.write(bytes);
os.close();
}
}
}