Skip to content

Commit cbf434c

Browse files
committed
feat: 可将 wss error 加入重连
1 parent 76d0fca commit cbf434c

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.kloping</groupId>
88
<artifactId>bot-qqpd-java</artifactId>
9-
<version>1.5.1-R3</version>
9+
<version>1.5.1-R4</version>
1010

1111
<packaging>jar</packaging>
1212
<name>bot-qqpd-java</name>
@@ -49,7 +49,7 @@
4949
<dependency>
5050
<groupId>io.github.Kloping</groupId>
5151
<artifactId>SpringTool</artifactId>
52-
<version>0.6.3-L1</version>
52+
<version>0.6.3-L2</version>
5353
<exclusions>
5454
<exclusion>
5555
<groupId>io.github.Kloping</groupId>

src/main/java/io/github/kloping/qqbot/Starter.java

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.github.kloping.qqbot.utils.LoggerImpl;
1212
import io.github.kloping.spt.StarterObjectApplication;
1313
import io.github.kloping.spt.annotations.Entity;
14+
import io.github.kloping.spt.interfaces.AutomaticWiringValue;
1415
import io.github.kloping.spt.interfaces.component.ContextManager;
1516
import io.github.kloping.spt.interfaces.component.HttpClientManager;
1617
import lombok.Data;
@@ -149,6 +150,14 @@ protected void after() {
149150
}
150151

151152
protected void wssWork() {
153+
AutomaticWiringValue automaticWiringValue = contextManager.getContextEntity(AutomaticWiringValue.class);
154+
if (config.getWebSocketListener() != null) {
155+
try {
156+
automaticWiringValue.wiring(config.getWebSocketListener(), contextManager);
157+
} catch (Exception e) {
158+
APPLICATION.logger.error(e.getMessage() + "\n\tat " + getExceptionLine(e));
159+
}
160+
}
152161
Future future = Public.EXECUTOR_SERVICE.submit(wssWorker);
153162
APPLICATION.INSTANCE.getContextManager().append(future, MAIN_FUTURE_ID);
154163
}

src/main/java/io/github/kloping/qqbot/impl/BaseConnectedEvent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public String getClassName() {
4141

4242
@Override
4343
public String toString() {
44-
return String.format("Bot(%s) Connected! By author kloping of bot-qqpd-java for version 1.5.1-R3", bot.getConfig().getAppid());
44+
return String.format("Bot(%s) Connected! By author kloping of bot-qqpd-java for version 1.5.1-R4", bot.getConfig().getAppid());
4545
}
4646

4747
@Override

src/main/java/io/github/kloping/qqbot/network/AuthAndHeartbeat.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void onClose(int code, WebSocketClient wss) {
7777
@AutoStand
7878
Config config;
7979

80-
private void identifyConnect(int code, WebSocketClient wss) {
80+
public void identifyConnect(int code, WebSocketClient wss) {
8181
if (!config.getReconnect()) return;
8282
Future future = contextManager.getContextEntity(Future.class, Starter.MAIN_FUTURE_ID);
8383
if (future != null && !future.isCancelled()) {

src/test/java/test_onError.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import io.github.kloping.qqbot.Starter;
2+
import io.github.kloping.qqbot.api.Intents;
3+
import io.github.kloping.qqbot.network.AuthAndHeartbeat;
4+
import io.github.kloping.qqbot.network.WebSocketListener;
5+
import io.github.kloping.spt.annotations.AutoStand;
6+
import org.java_websocket.client.WebSocketClient;
7+
8+
/**
9+
* 将 wss error 加入重连
10+
* @author github.kloping
11+
*/
12+
public class test_onError implements WebSocketListener {
13+
14+
@AutoStand
15+
AuthAndHeartbeat authAndHeartbeat;
16+
17+
@AutoStand
18+
Starter starter;
19+
20+
@Override
21+
public boolean onError(WebSocketClient client, Exception e) {
22+
//尝试重连
23+
authAndHeartbeat.identifyConnect(0, client);
24+
return WebSocketListener.super.onError(client, e);
25+
}
26+
27+
public static void main(String[] args) throws Exception {
28+
Starter starter = new Starter("xxx", "xxx");
29+
starter.getConfig().setCode(Intents.PRIVATE_INTENTS.getCode());
30+
//设置websocket 监听
31+
starter.getConfig().setWebSocketListener(new test_onError());
32+
//切换沙盒
33+
starter.getConfig().sandbox();
34+
starter.run();
35+
}
36+
}

0 commit comments

Comments
 (0)