Skip to content

Commit

Permalink
Merge pull request #15 from Enaium/develop
Browse files Browse the repository at this point in the history
Support reconnect
  • Loading branch information
Enaium authored Dec 24, 2024
2 parents a9aa6e1 + 5059076 commit 698cfe8
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
uses: gradle/gradle-build-action@v2
with:
arguments: build
gradle-version: 8.7
gradle-version: 8.12
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# kook-spring-boot-stater

![OpenJDK](https://img.shields.io/badge/OpenJDK-21-white?style=flat-square&logo=OpenJDK)
![SpringBoot](https://img.shields.io/badge/SpringBoot-3.2-green?style=flat-square&logo=SpringBoot)
![SpringBoot](https://img.shields.io/badge/SpringBoot-3.4-green?style=flat-square&logo=SpringBoot)
![Maven-Central](https://img.shields.io/maven-central/v/cn.enaium/kook-spring-boot-starter?style=flat-square)
![JitPack](https://img.shields.io/jitpack/version/com.github.Enaium/kook-spring-boot-starter?style=flat-square)
![GitHub](https://img.shields.io/github/license/enaium/kook-spring-boot-starter?style=flat-square)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ java {
}

group = "cn.enaium"
version = "0.4.2"
version = property("version").toString()

repositories {
mavenCentral()
Expand Down
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
springboot=3.2.4
annotations=24.1.0
springboot=3.4.1
annotations=24.1.0
version=0.5.0
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Mar 31 09:49:36 CST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
15 changes: 11 additions & 4 deletions src/main/java/cn/enaium/kookstarter/KookStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ public KookStarter(DefaultClient defaultClient) {

@Bean
public CommandLineRunner run() {

LOGGER.info("KookStarter is starting...");

return args -> defaultClient.connect();
return args -> {
Thread thread = new Thread(() -> {
try {
LOGGER.info("KookStarter启动中...");
defaultClient.connect().block();
} catch (Exception e) {
LOGGER.error("KookStarter启动失败: {}", e.getMessage());
}
});
thread.start();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
package cn.enaium.kookstarter.client.socket;

import cn.enaium.kookstarter.client.http.GatewayService;
import cn.enaium.kookstarter.model.response.GatewayIndexResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.net.URI;

Expand All @@ -29,6 +32,9 @@
*/
@Component
public class DefaultClient {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultClient.class);

private final GatewayService gatewayService;
private final DefaultHandler defaultHandler;

Expand All @@ -37,10 +43,17 @@ public DefaultClient(GatewayService gatewayService, @Lazy DefaultHandler default
this.defaultHandler = defaultHandler;
}

public void connect() {
final GatewayIndexResponse gatewayIndexResponse = gatewayService.gatewayIndex(0);
if (gatewayIndexResponse.code() == 0) {
new ReactorNettyWebSocketClient().execute(URI.create(gatewayIndexResponse.data().url()), defaultHandler).subscribe();
}
public Mono<Void> connect() {
return Mono.fromCallable(() -> gatewayService.gatewayIndex(0))
.subscribeOn(Schedulers.boundedElastic())
.flatMap(gatewayIndexResponse -> {
if (gatewayIndexResponse.code() == 0) {
LOGGER.info("获取网关地址成功: {}", gatewayIndexResponse.data().url());
return new ReactorNettyWebSocketClient().execute(URI.create(gatewayIndexResponse.data().url()), defaultHandler);
} else {
LOGGER.error("获取网关地址失败: {}", gatewayIndexResponse.message());
return connect();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class DefaultHandler implements WebSocketHandler {

public AtomicInteger sn = new AtomicInteger();

private long lastPong = System.currentTimeMillis();

public DefaultHandler(ObjectMapper objectMapper, ApplicationEventPublisher publisher, DefaultClient defaultClient, ApplicationContext applicationContext) {
this.objectMapper = objectMapper;
this.publisher = publisher;
Expand Down Expand Up @@ -84,13 +86,20 @@ public DefaultHandler(ObjectMapper objectMapper, ApplicationEventPublisher publi
switch (jsonNode.get("s").intValue()) {
case 1 -> //握手成功
LOGGER.info("连接建立成功");
case 3 -> {//收到pong

case 3 -> {//收到pong,延迟1分钟检查服务器是否响应
lastPong = System.currentTimeMillis();
Mono.delay(Duration.ofMinutes(1)).subscribe(delay -> {
if (System.currentTimeMillis() - lastPong > 30000) {
LOGGER.error("服务器未响应");
defaultClient.connect().doOnSuccess(it -> LOGGER.info("服务器未响应,重新连接")).subscribe();
}
});
}
case 5 -> {//要求客户端断开当前连接重新连接
LOGGER.info("服务器要求客户端断开当前连接重新连接");
sn.set(0);
session.close().doOnSuccess(unused -> defaultClient.connect()).subscribe();
session.close().block();
defaultClient.connect().subscribe();
}
case 0 -> {//事件
final var data = jsonNode.get("d");
Expand Down

0 comments on commit 698cfe8

Please sign in to comment.