Skip to content
Snippets Groups Projects
Commit 65653bb3 authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

Merge branch 'bugfix/14_fix_webserver' into 'main'

Problème réglé, reste incompatible en https.

See merge request SofianeLasri/sl-craft-plugin!1
parents 1591cf89 76cb9bf4
Branches
Tags
1 merge request!1Problème réglé, reste incompatible en https.
...@@ -19,6 +19,7 @@ public class internalWebServer { ...@@ -19,6 +19,7 @@ public class internalWebServer {
int serverPort = plugin.getConfig().getInt("internal-webserver-port"); int serverPort = plugin.getConfig().getInt("internal-webserver-port");
plugin.getServer().getConsoleSender().sendMessage("Lancement du serveur web intégré sur le port " + ChatColor.GOLD + serverPort); plugin.getServer().getConsoleSender().sendMessage("Lancement du serveur web intégré sur le port " + ChatColor.GOLD + serverPort);
plugin.getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "Attention! Le serveur ne fonctionne pas avec les requêtes https!");
// On fait un thread pour écouter le port // On fait un thread pour écouter le port
Runnable serverThread = () -> { Runnable serverThread = () -> {
try { try {
...@@ -46,13 +47,12 @@ public class internalWebServer { ...@@ -46,13 +47,12 @@ public class internalWebServer {
// reading. This means we don't mirror the contents of POST // reading. This means we don't mirror the contents of POST
// requests, for example. Note that the readLine() method // requests, for example. Note that the readLine() method
// works with Unix, Windows, and Mac line terminators. // works with Unix, Windows, and Mac line terminators.
String line; String line, commandName = "";
String[] aliases = new String[0];
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
if (line.length() == 0) if (line.equals("")) {
break; break;
//out.print(line + "\r\n"); }
//plugin.getServer().getConsoleSender().sendMessage(line);
// On va regarder si la ligne commence par GET // On va regarder si la ligne commence par GET
if (line.startsWith("GET")) { if (line.startsWith("GET")) {
// On split par les espaces // On split par les espaces
...@@ -61,14 +61,18 @@ public class internalWebServer { ...@@ -61,14 +61,18 @@ public class internalWebServer {
String command = split[1]; String command = split[1];
// On split par des / // On split par des /
String[] split2 = command.split("/"); aliases = command.split("/");
// On récupère le nom de la commande // On récupère le nom de la commande
String commandName = split2[1]; commandName = aliases[1];
// On ne process pas la commande ici car ça cause des problèmes vu qu'on va renvoyer le résultat avant que le client n'écoute
}
}
JSONObject answer = new JSONObject(); JSONObject answer = new JSONObject();
switch (commandName) { switch (commandName) {
case "discordMsg": case "discordMsg":
JSONObject json = (JSONObject) new JSONParser().parse(URLDecoder.decode(split2[2], "UTF-8")); JSONObject json = (JSONObject) new JSONParser().parse(URLDecoder.decode(aliases[2], "UTF-8"));
String message = json.get("message").toString(); String message = json.get("message").toString();
String playerName = json.get("playerName").toString(); String playerName = json.get("playerName").toString();
...@@ -81,14 +85,10 @@ public class internalWebServer { ...@@ -81,14 +85,10 @@ public class internalWebServer {
out.print(answer.toJSONString()); out.print(answer.toJSONString());
break; break;
case "getPlayers": case "getPlayers":
plugin.getServer().getConsoleSender().sendMessage("getPlayers");
// On renvoie la liste des joueurs // On renvoie la liste des joueurs
JSONObject listToReturn = new JSONObject(); JSONObject listToReturn = new JSONObject();
JSONObject players = new JSONObject(); JSONObject players = new JSONObject();
// On vérifie qu'il y a des joueurs -> pas vraiment utile vu que le timeout ne viens pas d'ici
//TODO: Corriger le timeout et supprimer la condition
if( plugin.getServer().getOnlinePlayers().size() > 0 ) {
for (Player p : plugin.getServer().getOnlinePlayers()) { for (Player p : plugin.getServer().getOnlinePlayers()) {
JSONObject playerInfos = new JSONObject(); JSONObject playerInfos = new JSONObject();
playerInfos.put("username", p.getName()); playerInfos.put("username", p.getName());
...@@ -96,7 +96,6 @@ public class internalWebServer { ...@@ -96,7 +96,6 @@ public class internalWebServer {
players.put(p.getName(), playerInfos); players.put(p.getName(), playerInfos);
} }
}
listToReturn.put("players", players); listToReturn.put("players", players);
out.print(listToReturn.toJSONString()); out.print(listToReturn.toJSONString());
break; break;
...@@ -106,8 +105,6 @@ public class internalWebServer { ...@@ -106,8 +105,6 @@ public class internalWebServer {
out.print(answer.toJSONString()); out.print(answer.toJSONString());
break; break;
} }
}
}
// Close socket, breaking the connection to the client, and // Close socket, breaking the connection to the client, and
// closing the input and output streams // closing the input and output streams
......
#Generated by Maven #Generated by Maven
#Mon May 02 23:05:52 CEST 2022 #Tue May 03 14:03:52 CEST 2022
groupId=com.slprojects groupId=com.slprojects
artifactId=SLCraftPlugin artifactId=SLCraftPlugin
version=1.6.0 version=1.6.0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment