-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 临时的崩溃修复 * 问题修复和代码格式化 现在发送消息不会导致服务器暂停 调整一些对象的作用域 * 现在游戏内聊天中的"[""]"不会被错误转义。 孩子们我回来了 * 异步游戏->QQ的CQ转义 反卡服 * 移除多余的代码 忘删了 * 添加一个替选的旧方法 把上一个提交删的加回来了 * APIUpdate 优化MessageThread效率 添加一个回调api。 * APIUpdate 清理MessageThread代码 添加一个自定义请求API。 * bug fix 修复游戏崩溃 * 自动重连更新 你说得对,但是我最开始是检测心跳包的。 * fix
- Loading branch information
Showing
9 changed files
with
181 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
fabric/src/main/java/cn/evole/mods/mcbot/util/onebot/BotUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
fabric/src/main/java/cn/evole/mods/mcbot/util/onebot/KeepAlive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cn.evole.mods.mcbot.util.onebot; | ||
|
||
import cn.evole.mods.mcbot.Const; | ||
import cn.evole.mods.mcbot.McBot; | ||
import cn.evole.mods.mcbot.command.ConnectCommand; | ||
import cn.evole.mods.mcbot.config.ModConfig; | ||
import cn.evole.onebot.sdk.event.meta.HeartbeatMetaEvent; | ||
import lombok.Getter; | ||
import lombok.val; | ||
import net.minecraft.ChatFormatting; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Queue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
@Getter | ||
public class KeepAlive { | ||
private final Queue<HeartbeatMetaEvent> heartBeatQueue = new LinkedBlockingQueue<>(5); | ||
// private long timeoutMillis = 100000; | ||
|
||
public KeepAlive() { | ||
} | ||
|
||
public void onHeartbeat(@NotNull HeartbeatMetaEvent event) { | ||
heartBeatQueue.add(event); | ||
} | ||
|
||
public void register() { | ||
while (true) { | ||
val limit = ModConfig.INSTANCE.getBotConfig().getMaxReconnectAttempts(); | ||
if (McBot.connected && ModConfig.INSTANCE.getBotConfig().isReconnect() && limit >= 1) { | ||
// try { | ||
// timeoutMillis = getHeartbeat(timeoutMillis + ModConfig.INSTANCE.getBotConfig().getTimeoutCompensation()).getInterval(); | ||
// } catch (TimeoutException e) { | ||
// reconnect(limit); | ||
// } | ||
if (McBot.onebot.getWs().isClosed()) { // 当你写完复杂的机制后突然发现有现成的api时 be like | ||
reconnect(limit); | ||
} | ||
} | ||
try { | ||
Thread.sleep(ModConfig.INSTANCE.getBotConfig().getTimeoutCompensation()); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} | ||
|
||
private void reconnect(final int limit) { | ||
int hasReconnect = 0; | ||
while (hasReconnect <= limit) { | ||
Const.LOGGER.info("正在尝试重连...第{}次", hasReconnect + 1); | ||
ConnectCommand.cqhttpDoConnect(); | ||
|
||
// try { | ||
// getHeartbeat(100000); | ||
// return; | ||
// } catch (TimeoutException ignored) {} | ||
try { | ||
Thread.sleep(ModConfig.INSTANCE.getBotConfig().getTimeoutCompensation()); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
if (McBot.onebot.getWs().isClosed()) hasReconnect++; | ||
else return; | ||
} | ||
Const.sendAllPlayerMsg("▌ " + ChatFormatting.RED + "群服互联意外断开,请联系服务器管理者。"); | ||
McBot.onebot.getWs().close(); | ||
McBot.connected = false; | ||
} | ||
|
||
/** | ||
* 尝试获取心跳包 | ||
* @param timeout 超时时间(毫秒) | ||
* @return 心跳包 | ||
*/ | ||
private HeartbeatMetaEvent getHeartbeat(final long timeout) throws TimeoutException { | ||
val startTime = System.currentTimeMillis(); | ||
while (System.currentTimeMillis() - startTime < timeout) { | ||
@Nullable HeartbeatMetaEvent event = heartBeatQueue.poll(); | ||
if (event == null && timeout > 100) { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} else { | ||
return event; | ||
} | ||
} | ||
throw new TimeoutException("未在指定时间内收到心跳包"); | ||
} | ||
} |
Oops, something went wrong.