Skip to content

Commit 75e5699

Browse files
committed
feat WebSocketListener See the code documentation for details;
fix #24; revise WebSocket ConnectionLostTimeout to 86 default.
1 parent 04039a8 commit 75e5699

File tree

7 files changed

+194
-11
lines changed

7 files changed

+194
-11
lines changed

docs/network.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
## 网络相关文档
2+
3+
4+
### 主要新增
5+
6+
```java
7+
8+
/**
9+
* WebSocketClient的监听处理
10+
* <br> 根据提示文档自行处理操作
11+
*
12+
* @author github.kloping
13+
*/
14+
public interface WebSocketListener {
15+
/**
16+
* @param client
17+
* @param handshake
18+
* @return 决定是否进行框架接下来的操作 true执行
19+
*/
20+
boolean onOpen(WebSocketClient client, ServerHandshake handshake)
21+
22+
/**
23+
* @param client
24+
* @param msg
25+
* @return 决定是否进行框架接下来的操作 true执行 此处若false则不执行(所有事件无法触发)
26+
*/
27+
boolean onMessage(WebSocketClient client, String msg)
28+
29+
/**
30+
* @param client
31+
* @param msg
32+
* @return 决定是否进行框架接下来的操作 true执行 若为false将无法发送消息
33+
*/
34+
boolean onSend(WebSocketClient client, String msg)
35+
36+
/**
37+
* Called after the websocket connection has been closed.
38+
*
39+
* @param client
40+
* @param code The codes can be looked up here: {@link CloseFrame}
41+
* @param reason Additional information string
42+
* @param remote Returns whether or not the closing of the connection was initiated by the remote
43+
* host.
44+
* @return 决定是否进行框架接下来的操作 true执行 若为false将不自动重连
45+
*/
46+
boolean onClose(WebSocketClient client, int code, String reason, boolean remote)
47+
48+
/**
49+
* @param client
50+
* @param e
51+
* @return 同上 框架操作仅日志
52+
*/
53+
boolean onError(WebSocketClient client, Exception e)
54+
}
55+
56+
```
57+
58+
> 使用示例
59+
60+
```java
61+
public class demo{
62+
public static void main(String[] args) {
63+
starter.getConfig().setWebSocketListener(new WebSocketListener() {
64+
@Override
65+
public boolean onOpen(WebSocketClient client, ServerHandshake handshake) {
66+
client.setConnectionLostTimeout(10);
67+
System.out.println("ws打开了");
68+
return true;
69+
}
70+
71+
@Override
72+
public boolean onMessage(WebSocketClient client, String msg) {
73+
System.out.println("ws接收到消息了");
74+
return true;
75+
}
76+
77+
@Override
78+
public boolean onSend(WebSocketClient client, String msg) {
79+
System.out.println("ws将发送消息");
80+
return true;
81+
}
82+
83+
@Override
84+
public boolean onClose(WebSocketClient client, int code, String reason, boolean remote) {
85+
System.out.println("ws关闭了");
86+
return true;
87+
}
88+
89+
@Override
90+
public boolean onError(WebSocketClient client, Exception e) {
91+
System.out.println("ws内部发送报错");
92+
return true;
93+
}
94+
});
95+
}
96+
}
97+
```

docs/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ _**待完善..**_
2121
- **[事件 event](event.md)**
2222
- **[消息 message](message.md)**
2323
- **[动作 action](action.md)**
24+
- **[网络相关设置](network.md)**
2425

2526
<hr>
2627

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-Beta2</version>
9+
<version>1.5.1-L1</version>
1010

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

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

+3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import io.github.kloping.qqbot.impl.ListenerHost;
1111
import io.github.kloping.qqbot.interfaces.ImageUploadInterceptor;
1212
import io.github.kloping.qqbot.network.Events;
13+
import io.github.kloping.qqbot.network.WebSocketListener;
1314
import io.github.kloping.qqbot.network.WssWorker;
1415
import io.github.kloping.qqbot.utils.LoggerImpl;
1516
import lombok.Data;
1617
import lombok.Getter;
18+
import org.java_websocket.client.WebSocketClient;
1719

1820
import java.util.HashSet;
1921
import java.util.Set;
@@ -187,6 +189,7 @@ public static class Config {
187189
private Boolean reconnect = true;
188190
private Set<ListenerHost> listenerHosts = new HashSet<>();
189191
private ImageUploadInterceptor interceptor0;
192+
private WebSocketListener webSocketListener;
190193
}
191194

192195
public Bot getBot() {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ 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-Beta2", bot.getConfig().getAppid());
44+
return String.format("Bot(%s) Connected! By author kloping of bot-qqpd-java for version 1.5.1-L1", bot.getConfig().getAppid());
4545
}
4646
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.github.kloping.qqbot.network;
2+
3+
import org.java_websocket.client.WebSocketClient;
4+
import org.java_websocket.framing.CloseFrame;
5+
import org.java_websocket.handshake.ServerHandshake;
6+
7+
/**
8+
* WebSocketClient的监听处理
9+
* <br> 根据提示文档自行处理操作
10+
*
11+
* @author github.kloping
12+
*/
13+
public interface WebSocketListener {
14+
/**
15+
* @param client
16+
* @param handshake
17+
* @return 决定是否进行框架接下来的操作 true执行
18+
*/
19+
default boolean onOpen(WebSocketClient client, ServerHandshake handshake) {
20+
return true;
21+
}
22+
23+
/**
24+
* @param client
25+
* @param msg
26+
* @return 决定是否进行框架接下来的操作 true执行 此处若false则不执行(所有事件无法触发)
27+
*/
28+
default boolean onMessage(WebSocketClient client, String msg) {
29+
return true;
30+
}
31+
32+
/**
33+
* @param client
34+
* @param msg
35+
* @return 决定是否进行框架接下来的操作 true执行 若为false将无法发送消息
36+
*/
37+
default boolean onSend(WebSocketClient client, String msg) {
38+
return true;
39+
}
40+
41+
/**
42+
* Called after the websocket connection has been closed.
43+
*
44+
* @param client
45+
* @param code The codes can be looked up here: {@link CloseFrame}
46+
* @param reason Additional information string
47+
* @param remote Returns whether or not the closing of the connection was initiated by the remote
48+
* host.
49+
* @return 决定是否进行框架接下来的操作 true执行 若为false将不自动重连
50+
*/
51+
default boolean onClose(WebSocketClient client, int code, String reason, boolean remote) {
52+
return true;
53+
}
54+
55+
/**
56+
* @param client
57+
* @param e
58+
* @return 同上 框架操作仅日志
59+
*/
60+
default boolean onError(WebSocketClient client, Exception e) {
61+
return true;
62+
}
63+
}

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

+28-9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public class WssWorker implements Runnable {
3838
@AutoStand
3939
private Logger logger;
4040

41+
@AutoStand
42+
Starter.Config config;
43+
4144
public WebSocketClient webSocket;
4245

4346
protected Integer msgr = 0;
@@ -62,11 +65,15 @@ public void run() {
6265

6366
@Override
6467
public void onOpen(ServerHandshake serverHandshake) {
68+
if (preMethods(serverHandshake)) return;
6569
logger.info("wss opened");
6670
}
6771

6872
@Override
6973
public void onMessage(String s) {
74+
if (config.getWebSocketListener() != null)
75+
if (!config.getWebSocketListener().onMessage(webSocket, s))
76+
return;
7077
Pack pack = GSON.fromJson(s, Pack.class);
7178
logger.log(String.format("receive %s", s));
7279
if (pack == null) {
@@ -80,14 +87,16 @@ public void onMessage(String s) {
8087
}
8188

8289
@Override
83-
public void send(String text) throws NotYetConnectedException {
84-
super.send(text);
85-
logger.log("wss send: " + text);
90+
public void send(String msg) throws NotYetConnectedException {
91+
if (preMethods(msg)) return;
92+
super.send(msg);
93+
logger.log("wss send: " + msg);
8694
msgs++;
8795
}
8896

8997
@Override
9098
public void onClose(int i, String s, boolean b) {
99+
if (preMethods(i, s, b)) return;
91100
logger.waring("wss closed with code " + i + " " + s);
92101
for (OnCloseListener onCloseListener : closeListeners) {
93102
onCloseListener.onClose(i, webSocket);
@@ -96,12 +105,13 @@ public void onClose(int i, String s, boolean b) {
96105

97106
@Override
98107
public void onError(Exception e) {
108+
if (preMethods(e)) return;
99109
logger.error("wss error");
100110
e.printStackTrace();
101111
}
102112
};
103113
//两次心跳的事件
104-
webSocket.setConnectionLostTimeout(120);
114+
webSocket.setConnectionLostTimeout(86);
105115
webSocket.run();
106116
} catch (Exception e) {
107117
logger.error("在WebSocketClient启动时失败");
@@ -115,11 +125,8 @@ public void onError(Exception e) {
115125
run();
116126
}
117127
}
118-
119-
@AutoStand
120-
Starter.Config config;
121-
122128
public List<OnCloseListener> closeListeners = new ArrayList<>();
129+
123130
public List<OnPackReceive> onPackReceives = new LinkedList<>();
124131

125132
public Boolean getReconnect() {
@@ -135,6 +142,18 @@ public List<OnPackReceive> getOnPackReceives() {
135142
return onPackReceives;
136143
}
137144

138-
public void close() {
145+
private boolean preMethods(Object... objects) {
146+
WebSocketListener listener = config.getWebSocketListener();
147+
if (listener == null) return false;
148+
Object o1 = objects[0];
149+
if (o1 instanceof Exception) {
150+
return !listener.onError(webSocket, (Exception) o1);
151+
} else if (o1 instanceof String) {
152+
return !listener.onSend(webSocket, o1.toString());
153+
} else if (o1 instanceof ServerHandshake) {
154+
return !listener.onOpen(webSocket, (ServerHandshake) o1);
155+
} else if (o1 instanceof Integer) {
156+
return !listener.onClose(webSocket, (Integer) objects[0], (String) objects[1], (boolean) objects[2]);
157+
} else return false;
139158
}
140159
}

0 commit comments

Comments
 (0)