Skip to content

Commit d75cf31

Browse files
committed
feat: 将1011加入重连
1 parent 9c70f84 commit d75cf31

14 files changed

+153
-36
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Maven
1818
<dependency>
1919
<groupId>io.github.kloping</groupId>
2020
<artifactId>bot-qqpd-java</artifactId>
21-
<version>1.5.1-R4</version>
21+
<version>1.5.1</version>
2222
</dependency>
2323
```
2424

pom.xml

+1-1
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-R4</version>
9+
<version>1.5.1</version>
1010

1111
<packaging>jar</packaging>
1212
<name>bot-qqpd-java</name>

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

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package io.github.kloping.qqbot;
22

3+
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.JSONObject;
35
import io.github.kloping.common.Public;
46
import io.github.kloping.qqbot.api.BotContent;
7+
import io.github.kloping.qqbot.api.exc.RequestException;
58
import io.github.kloping.qqbot.entities.Bot;
9+
import io.github.kloping.qqbot.entities.exc.QBotError;
610
import io.github.kloping.qqbot.entities.qqpd.message.RawMessage;
711
import io.github.kloping.qqbot.http.data.ActionResult;
8-
import io.github.kloping.qqbot.utils.RequestException;
912
import io.github.kloping.spt.annotations.AutoStand;
1013
import io.github.kloping.spt.annotations.AutoStandAfter;
1114
import io.github.kloping.spt.annotations.Entity;
@@ -18,6 +21,7 @@
1821
import org.jsoup.nodes.Document;
1922

2023
import java.awt.*;
24+
import java.lang.reflect.Constructor;
2125
import java.lang.reflect.Method;
2226

2327
/**
@@ -45,7 +49,6 @@ public void after(HttpClientManager manager) {
4549
public void receive(HttpClientManager manager, String url, Integer code, Class<?> interface0, Method method,
4650
Connection.Method reqMethod, Class<?> cla, Object o, Document metadata) {
4751
if (o == null || code == null || metadata == null) return;
48-
4952
logger.log(String.format("Use the (%s) method through the (%s) interface to request " +
5053
"the data obtained by the response code of the (%s) URL is (%s), " +
5154
"and (%s) may be converted to (%s) type Will be processed and filtered",
@@ -78,10 +81,29 @@ public void receive(HttpClientManager manager, String url, Integer code, Class<?
7881
}
7982
});
8083
if (code >= 400 || code < 200) {
81-
throw new RequestException(code, metadata.body().wholeText(), url, method.getName());
84+
RequestException requestException = null;
85+
String bodyJson = metadata.body().wholeText();
86+
JSONObject exjo = JSON.parseObject(bodyJson);
87+
QBotError error = exjo.toJavaObject(QBotError.class);
88+
//加入exc对象
89+
int eccode = exjo.getInteger("code");
90+
if (Resource.CODE2EXCEPTION.containsKey(eccode)) {
91+
try {
92+
Class<? extends RequestException> exceptionClass = Resource.CODE2EXCEPTION.get(eccode);
93+
Constructor constructor = exceptionClass.getConstructor(int.class, String.class, String.class, String.class);
94+
requestException = (RequestException) constructor.newInstance(eccode, bodyJson, url, method.getName());
95+
} catch (Exception e) {
96+
logger.error(e.getMessage());
97+
}
98+
} else {
99+
requestException = new RequestException(eccode, bodyJson, url, method.getName());
100+
}
101+
if (requestException != null) requestException.setData(error);
102+
throw requestException;
82103
}
83104
}
84105

106+
85107
public void fillAll(Class<?> cla, Object o) {
86108
if (o instanceof BotContent) {
87109
BotContent content = (BotContent) o;
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
package io.github.kloping.qqbot;
22

33
import com.google.gson.Gson;
4+
import io.github.kloping.qqbot.api.exc.RequestException;
5+
import io.github.kloping.qqbot.impl.exc.InvalidRequestException;
46
import io.github.kloping.spt.annotations.Entity;
57

8+
import java.util.HashMap;
9+
import java.util.Map;
10+
611
/**
712
* @author github.kloping
813
*/
914
@Entity
1015
public class Resource {
1116
public static final Gson GSON = new Gson();
17+
18+
public static final Map<Integer, Class<? extends RequestException>> CODE2EXCEPTION = new HashMap<>();
19+
20+
static {
21+
Resource.CODE2EXCEPTION.put(11255, InvalidRequestException.class);
22+
Resource.CODE2EXCEPTION.put(11244, InvalidRequestException.class);
23+
}
1224
}

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

-6
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ public class Starter implements Runnable {
8282
public static final String RECONNECT_K_ID = "is_reconnect";
8383
public static final String CONFIG_ID = "config";
8484

85-
public static final Integer CODE_4006 = 4006;
86-
public static final Integer CODE_4007 = 4007;
87-
public static final Integer CODE_4008 = 4008;
88-
public static final Integer CODE_4009 = 4009;
89-
public static final Integer CODE_4900 = 4900;
90-
public static final Integer CODE_4913 = 4913;
9185

9286
@Getter
9387
private Config config = new Config();

src/main/java/io/github/kloping/qqbot/api/Intents.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package io.github.kloping.qqbot.api;
22

33
/**
4-
* <pre><code>
5-
*
6-
* GUILDS (1 &lt;&lt; 0)
4+
* <pre class="language-yaml codecopy-enabled"><code>GUILDS (1 &lt;&lt; 0)
75
* <span class="token punctuation">-</span> GUILD_CREATE // 当机器人加入新guild时
86
* <span class="token punctuation">-</span> GUILD_UPDATE // 当guild资料发生变更时
97
* <span class="token punctuation">-</span> GUILD_DELETE // 当机器人退出guild时
@@ -28,18 +26,18 @@
2826
* <span class="token punctuation">-</span> DIRECT_MESSAGE_CREATE // 当收到用户发给机器人的私信消息时
2927
* <span class="token punctuation">-</span> DIRECT_MESSAGE_DELETE // 删除(撤回)消息事件
3028
*
31-
* OPEN_FORUMS_EVENT (1 &lt;&lt; 18) // 论坛事件<span class="token punctuation">,</span> 此为公域的论坛事件
32-
* <span class="token punctuation">-</span> OPEN_FORUM_THREAD_CREATE // 当用户创建主题时
33-
* <span class="token punctuation">-</span> OPEN_FORUM_THREAD_UPDATE // 当用户更新主题时
34-
* <span class="token punctuation">-</span> OPEN_FORUM_THREAD_DELETE // 当用户删除主题时
35-
* <span class="token punctuation">-</span> OPEN_FORUM_POST_CREATE // 当用户创建帖子时
36-
* <span class="token punctuation">-</span> OPEN_FORUM_POST_DELETE // 当用户删除帖子时
37-
* <span class="token punctuation">-</span> OPEN_FORUM_REPLY_CREATE // 当用户回复评论时
38-
* <span class="token punctuation">-</span> OPEN_FORUM_REPLY_DELETE // 当用户删除评论时
3929
*
40-
* AUDIO_OR_LIVE_CHANNEL_MEMBER (1 &lt;&lt; 19) // 音视频/直播子频道成员进出事件
41-
* <span class="token punctuation">-</span> AUDIO_OR_LIVE_CHANNEL_MEMBER_ENTER // 当用户进入音视频/直播子频道
42-
* <span class="token punctuation">-</span> AUDIO_OR_LIVE_CHANNEL_MEMBER_EXIT // 当用户离开音视频/直播子频道
30+
* GROUP_AND_C2C_EVENT (1 &lt;&lt; 25)
31+
* <span class="token punctuation">-</span> C2C_MESSAGE_CREATE // 用户单聊发消息给机器人时候
32+
* <span class="token punctuation">-</span> FRIEND_ADD // 用户添加使用机器人
33+
* <span class="token punctuation">-</span> FRIEND_DEL // 用户删除机器人
34+
* <span class="token punctuation">-</span> C2C_MSG_REJECT // 用户在机器人资料卡手动关闭"主动消息"推送
35+
* <span class="token punctuation">-</span> C2C_MSG_RECEIVE // 用户在机器人资料卡手动开启"主动消息"推送开关
36+
* <span class="token punctuation">-</span> GROUP_AT_MESSAGE_CREATE // 用户在群里@机器人时收到的消息
37+
* <span class="token punctuation">-</span> GROUP_ADD_ROBOT // 机器人被添加到群聊
38+
* <span class="token punctuation">-</span> GROUP_DEL_ROBOT // 机器人被移出群聊
39+
* <span class="token punctuation">-</span> GROUP_MSG_REJECT // 群管理员主动在机器人资料页操作关闭通知
40+
* <span class="token punctuation">-</span> GROUP_MSG_RECEIVE // 群管理员主动在机器人资料页操作开启通知
4341
*
4442
* INTERACTION (1 &lt;&lt; 26)
4543
* <span class="token punctuation">-</span> INTERACTION_CREATE // 互动事件创建时
@@ -55,7 +53,7 @@
5553
* <span class="token punctuation">-</span> FORUM_POST_CREATE // 当用户创建帖子时
5654
* <span class="token punctuation">-</span> FORUM_POST_DELETE // 当用户删除帖子时
5755
* <span class="token punctuation">-</span> FORUM_REPLY_CREATE // 当用户回复评论时
58-
* <span class="token punctuation">-</span> FORUM_REPLY_DELETE // 当用户删除评论时
56+
* <span class="token punctuation">-</span> FORUM_REPLY_DELETE // 当用户回复评论时
5957
* <span class="token punctuation">-</span> FORUM_PUBLISH_AUDIT_RESULT // 当用户发表审核通过时
6058
*
6159
* AUDIO_ACTION (1 &lt;&lt; 29)
@@ -68,7 +66,6 @@
6866
* <span class="token punctuation">-</span> AT_MESSAGE_CREATE // 当收到@机器人的消息时
6967
* <span class="token punctuation">-</span> PUBLIC_MESSAGE_DELETE // 当频道的消息被删除时
7068
* </code><i class="code-copy" title="Copy to clipboard"><svg style="color:#aaa;font-size:14px" t="1572422231464" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3201" width="14" height="14"><path d="M866.461538 39.384615H354.461538c-43.323077 0-78.769231 35.446154-78.76923 78.769231v39.384616h472.615384c43.323077 0 78.769231 35.446154 78.769231 78.76923v551.384616h39.384615c43.323077 0 78.769231-35.446154 78.769231-78.769231V118.153846c0-43.323077-35.446154-78.769231-78.769231-78.769231z m-118.153846 275.692308c0-43.323077-35.446154-78.769231-78.76923-78.769231H157.538462c-43.323077 0-78.769231 35.446154-78.769231 78.769231v590.769231c0 43.323077 35.446154 78.769231 78.769231 78.769231h512c43.323077 0 78.769231-35.446154 78.76923-78.769231V315.076923z m-354.461538 137.846154c0 11.815385-7.876923 19.692308-19.692308 19.692308h-157.538461c-11.815385 0-19.692308-7.876923-19.692308-19.692308v-39.384615c0-11.815385 7.876923-19.692308 19.692308-19.692308h157.538461c11.815385 0 19.692308 7.876923 19.692308 19.692308v39.384615z m157.538461 315.076923c0 11.815385-7.876923 19.692308-19.692307 19.692308H216.615385c-11.815385 0-19.692308-7.876923-19.692308-19.692308v-39.384615c0-11.815385 7.876923-19.692308 19.692308-19.692308h315.076923c11.815385 0 19.692308 7.876923 19.692307 19.692308v39.384615z m78.769231-157.538462c0 11.815385-7.876923 19.692308-19.692308 19.692308H216.615385c-11.815385 0-19.692308-7.876923-19.692308-19.692308v-39.384615c0-11.815385 7.876923-19.692308 19.692308-19.692308h393.846153c11.815385 0 19.692308 7.876923 19.692308 19.692308v39.384615z" p-id="3202"></path></svg></i></pre>
71-
*
7269
* @author github.kloping
7370
*/
7471
public enum Intents {

src/main/java/io/github/kloping/qqbot/utils/RequestException.java src/main/java/io/github/kloping/qqbot/api/exc/RequestException.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package io.github.kloping.qqbot.utils;
1+
package io.github.kloping.qqbot.api.exc;
22

3+
import io.github.kloping.qqbot.entities.exc.QBotError;
34
import lombok.Getter;
5+
import lombok.Setter;
46

57
/**
68
* 发送请求时抛出的运行是异常
@@ -14,6 +16,8 @@ public class RequestException extends RuntimeException {
1416
private final String body;
1517
private final String url;
1618
private final String method;
19+
@Setter
20+
protected QBotError data;
1721

1822
public RequestException(int code, String body, String url,String method) {
1923
super(String.format("The request error response url [%s] code is [%s] (%s)", url, code, body));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.kloping.qqbot.entities.exc;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* @author github.kloping
7+
*/
8+
@Data
9+
public class QBotError {
10+
private String message;
11+
private Integer code;
12+
private Integer err_code;
13+
private String trace_id;
14+
}

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-R4", bot.getConfig().getAppid());
44+
return String.format("Bot(%s) Connected! By author kloping of bot-qqpd-java for version 1.5.1", bot.getConfig().getAppid());
4545
}
4646

4747
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.github.kloping.qqbot.impl.exc;
2+
3+
import io.github.kloping.qqbot.Resource;
4+
import io.github.kloping.qqbot.api.exc.RequestException;
5+
import io.github.kloping.qqbot.entities.exc.QBotError;
6+
7+
/**
8+
* @author github.kloping
9+
*/
10+
public class InvalidRequestException extends RequestException {
11+
12+
13+
private QBotError error;
14+
15+
public InvalidRequestException(int code, String body, String url, String method) {
16+
super(code, body, url, method);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.github.kloping.qqbot.impl.exc;
2+
3+
import io.github.kloping.qqbot.Resource;
4+
import io.github.kloping.qqbot.api.exc.RequestException;
5+
import io.github.kloping.qqbot.entities.exc.QBotError;
6+
7+
/**
8+
* @author github.kloping
9+
*/
10+
public class TokenExpireException extends RequestException {
11+
12+
13+
private QBotError error;
14+
15+
public TokenExpireException(int code, String body, String url, String method) {
16+
super(code, body, url, method);
17+
}
18+
}

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

+31-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
import com.alibaba.fastjson.JSON;
44
import com.alibaba.fastjson.JSONObject;
5-
import io.github.kloping.spt.annotations.AutoStand;
6-
import io.github.kloping.spt.annotations.AutoStandAfter;
7-
import io.github.kloping.spt.annotations.Entity;
8-
import io.github.kloping.spt.interfaces.Logger;
9-
import io.github.kloping.spt.interfaces.component.ContextManager;
105
import io.github.kloping.common.Public;
116
import io.github.kloping.date.FrameUtils;
127
import io.github.kloping.qqbot.Starter;
@@ -17,6 +12,12 @@
1712
import io.github.kloping.qqbot.impl.BaseConnectedEvent;
1813
import io.github.kloping.qqbot.interfaces.OnCloseListener;
1914
import io.github.kloping.qqbot.interfaces.OnPackReceive;
15+
import io.github.kloping.spt.annotations.AutoStand;
16+
import io.github.kloping.spt.annotations.AutoStandAfter;
17+
import io.github.kloping.spt.annotations.Entity;
18+
import io.github.kloping.spt.interfaces.Logger;
19+
import io.github.kloping.spt.interfaces.component.ContextManager;
20+
import lombok.extern.slf4j.Slf4j;
2021
import org.java_websocket.client.WebSocketClient;
2122

2223
import java.util.concurrent.Future;
@@ -28,8 +29,21 @@
2829
/**
2930
* @author github.kloping
3031
*/
32+
@Slf4j
3133
@Entity
3234
public class AuthAndHeartbeat implements OnPackReceive, OnCloseListener, Events.EventRegister {
35+
36+
public static final int CODE_4006 = 4006;
37+
public static final int CODE_4007 = 4007;
38+
public static final int CODE_4008 = 4008;
39+
public static final int CODE_4009 = 4009;
40+
public static final int CODE_4900 = 4900;
41+
public static final int CODE_4913 = 4913;
42+
/**
43+
* 服务器 内部异常
44+
*/
45+
public static final int CODE_1011 = 1011;
46+
3347
@AutoStand
3448
Logger logger;
3549

@@ -67,6 +81,18 @@ public void onClose(int code, WebSocketClient wss) {
6781
// https://github.com/TooTallNate/Java-WebSocket/wiki/Lost-connection-detection
6882
identifyConnect(code, wss);
6983
break;
84+
case CODE_1011:
85+
Public.EXECUTOR_SERVICE.execute(() -> {
86+
logger.error("websocket closed with code 1011,server internal exception");
87+
logger.error("reconnect in 3 seconds");
88+
try {
89+
TimeUnit.SECONDS.sleep(3);
90+
} catch (InterruptedException e) {
91+
logger.error(e.getMessage());
92+
}
93+
identifyConnect(code, wss);
94+
});
95+
break;
7096
default:
7197
logger.error(String.format("暂未处理的异常code(%s)", code));
7298
break;

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.github.kloping.map.MapUtils;
77
import io.github.kloping.qqbot.Starter;
88
import io.github.kloping.qqbot.api.event.Event;
9+
import io.github.kloping.qqbot.api.exc.RequestException;
910
import io.github.kloping.qqbot.api.message.MessageEvent;
1011
import io.github.kloping.qqbot.entities.Bot;
1112
import io.github.kloping.qqbot.entities.Pack;
@@ -92,8 +93,18 @@ private void onEvent(String t, JSONObject obj) throws Exception {
9293
logger.error("EventReceiver The method parameter is set incorrectly");
9394
logger.error(e.getMessage() + "\n\tat " + getExceptionLine(e));
9495
} catch (InvocationTargetException e) {
95-
if (l.handleException(e.getTargetException()))
96-
logger.error(getExceptionLine(e.getTargetException()));
96+
if (l.handleException(e.getTargetException())) {
97+
if (e.getTargetException() instanceof RequestException) {
98+
RequestException re = (RequestException) e.getTargetException();
99+
logger.error(String.format("%s: code(%s) %s at", re.getClass().getSimpleName(), re.getCode(), re.getData().getMessage()));
100+
for (StackTraceElement traceElement : re.getStackTrace()) {
101+
logger.error(String.format("\t%s.%s(%s:%s)", traceElement.getClassName(), traceElement.getMethodName(),
102+
traceElement.getFileName() == null ? "unknown" : traceElement.getFileName(), traceElement.getLineNumber()));
103+
}
104+
} else {
105+
logger.error(getExceptionLine(e.getTargetException()));
106+
}
107+
}
97108
} catch (Exception e) {
98109
if (l.handleException(e))
99110
logger.error(getExceptionLine(e));

src/test/java/test_main.java

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import io.github.kloping.qqbot.Starter;
22
import io.github.kloping.qqbot.api.Intents;
3+
import io.github.kloping.qqbot.entities.qqpd.Dms;
34

45
/**
56
* @author github.kloping

0 commit comments

Comments
 (0)