Skip to content

Commit

Permalink
修复重复连接问题
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdoeswhat committed Apr 25, 2022
1 parent 3e9104c commit 83c8a8e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>me.albert</groupId>
<artifactId>AmazingBot</artifactId>
<packaging>jar</packaging>
<version>4.0.9</version>
<version>4.0.10</version>
<name>AmazingBot</name>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: AmazingBot
version: 4.0.9
version: 4.0.10
author: Albert
main: me.albert.amazingbot.AmazingBot
api-version: 1.13
Expand Down
3 changes: 3 additions & 0 deletions src/me/albert/amazingbot/bot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public static void setApi(BotAPI api) {
Bot.api = api;
}

public static BotClient getClient() {
return client;
}

public static Boolean getConnected() {
return !client.isClosed();
Expand Down
18 changes: 10 additions & 8 deletions src/me/albert/amazingbot/bot/BotClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public JsonObject processMessageSend(JsonObject object, int timeout) {

@Override
public void onMessage(String msg) {
if (!instance.isEnabled()) {
if (!instance.isEnabled() || Bot.getClient() != this) {
this.close();
return;
}
Expand Down Expand Up @@ -143,6 +143,9 @@ public void processMessageRec(String msg) {

@Override
public void onOpen(ServerHandshake handshakedata) {
if (Bot.getClient() != this) {
this.close();
}
AmazingBot.getInstance().getLogger().info("§a机器人连接成功!");
callEvent(new WebSocketConnectedEvent());
}
Expand All @@ -152,15 +155,14 @@ public void onOpen(ServerHandshake handshakedata) {
public void onClose(int code, String reason, boolean remote) {
AmazingBot.getInstance().getLogger().warning("机器人连接关闭: " + (remote ? "remote peer" : "us") + " Code: " + code + " Reason: " + reason);
int delay = instance.getConfig().getInt("main.auto_reconnect");
synchronized (AmazingBot.getInstance()) {
if (instance.isEnabled() && code != 1000) {
if (this.isOpen()) {
if (instance.isEnabled() && code != 1000) {
AmazingBot.getInstance().getLogger().info("§a将在" + delay + "秒后再次尝试连接");
taskID = Bukkit.getScheduler().runTaskLater(instance, () -> {
if (Bot.getClient() != this) {
return;
}
AmazingBot.getInstance().getLogger().info("§a将在" + delay + "秒后再次尝试连接");
Bukkit.getScheduler().cancelTask(taskID);
taskID = Bukkit.getScheduler().runTaskLater(instance, this::reconnect, 20L * delay).getTaskId();
}
reconnect();
}, 20L * delay).getTaskId();
}
}

Expand Down
1 change: 0 additions & 1 deletion src/me/albert/amazingbot/utils/MsgUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public static String deFormatCQCode(String str) {
}



/**
* @param file 绝对路径,例如 file:///C:\\Users\Richard\Pictures\1.png
* 网络 URL,例如 http://i1.piimg.com/567571/fdd6e7b6d93f1ef0.jpg
Expand Down

0 comments on commit 83c8a8e

Please sign in to comment.