Skip to content

Commit

Permalink
feat: add reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed Dec 5, 2022
1 parent 5e92229 commit 3f1748c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/kr/minehub/servers/agent/command/MinehubCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public static boolean onCommand(CommandSender sender, Command command, String la
return runDebug(sender, args.length >= 2 ? args[1] : null);
case INFO:
return sendServerAgentStatus(sender);
case RECONNECT:
return tryReconnect(sender);
case HELP:
default:
sender.sendMessage(
Expand All @@ -117,6 +119,31 @@ public static boolean onCommand(CommandSender sender, Command command, String la
return false;
}

private static boolean tryReconnect(CommandSender sender) {
if (MinehubCommand.hasPermission(sender, "reconnect")) {
sender.sendMessage(
AgentLogger.log("Minehub RSM 서버와 다시 연결을 시도합니다.")
);

try {
Main.core.server.getWebsocketSession().forceReconnect();
} catch(Exception e) {
sender.sendMessage(
AgentLogger.error("Minehub RSM 서버와 연결에 실패했습니다.")
);

return true;
}

sender.sendMessage(
AgentLogger.log("Minehub RSM 서버와 다시 연결 되었습니다.")
);
} else {
sender.sendMessage(AgentLogger.error("에러: 권한이 없습니다."));
}
return true;
}

private static boolean sendServerAgentStatus(CommandSender sender) {
if (MinehubCommand.hasPermission(sender, "info")) {
sender.sendMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum MinehubCommandAction {
RENAME("rename", "<name>", "Minehub ServerAgent에 등록된 이 서버의 이름을 변경합니다."),
INFO("info", "", "ServerAgent의 현재 상태를 표시합니다."),
DEBUG("debug", "<? command>", "ServerAgent를 디버깅 하기 위한 명령을 실행 합니다"),
RECONNECT("reconnect", "", "오류 등으로 인해 Minehub RSM 서버와 연결이 끊긴 경우, 강제로 다시 연결을 시도합니다"),
TOKEN("token", "", "(Debug) oAuth2 인증용 토큰을 표시합니다.");

String cmdline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ public boolean isConnecting() {
return !this.isConnected() && this.isConnecting;
}

public void forceReconnect() throws IOException, InvalidRefreshTokenException, WebSocketException {
Bukkit.getLogger().info(AgentLogger.log("Minehub 서버에 강제로 연결을 다시 시작합니다."));
if (this.isConnecting) {
Bukkit.getLogger().warning(AgentLogger.warn("Minehub 연결이 연결된 상태로 인식되고 있습니다. 강제로 연결을 해제한 후 다시 연결을 시도합니다."));
try {
this.disconnect();
} catch(Exception e) {}

this.isConnecting = false;
}

this.connect();
Bukkit.getLogger().info(AgentLogger.log("Minehub 서버에 다시 연결되었습니다."));
}

public void sendMessage(String content) {
ws.sendText(content);
}
Expand Down

0 comments on commit 3f1748c

Please sign in to comment.