Skip to content

Commit

Permalink
add config to disable ws heartbeat check strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Dec 3, 2024
1 parent ca8df7c commit ab225b4
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 14 deletions.
21 changes: 11 additions & 10 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

## overflow.json

|| 默认值 | 注释 |
|--------------------|-----------------------|------------------------------------------------------------------------------------------------------|
| `ws_host` | `ws://127.0.0.1:5800` | 正向WebSocket连接地址 |
| `reversed_ws_port` | `-1` | 反向WebSocket服务器端口,`-1` 为不开启反向WS。开启反向WS将会禁用正向WS连接 |
| `token` | | 鉴权令牌,留空则不进行鉴权 |
| `no_platform` | `false` | 是否禁止 Overflow 调用需要账号凭证的网络接口,如获取分享Key、群活跃等。在**非QQ平台****无法获取账号凭证**的平台 (如 Gensokyo) 使用 Overflow 请开启该选项 |
| `use_cq_code` | `false` | 发送消息时,是否使用 CQ 码格式 |
| `retry_times` | `5` | 正向WS断开连接后重连尝试次数,设为 `-1` 禁用自动重连 |
| `retry_wait_mills` | `5000` | 每次重连之间的间隔时间 (毫秒) |
| `retry_rest_mills` | `60000` | 重连次数用完后,等待多长时间 (毫秒) 后,重置次数再进行重连。设为 `-1` 禁用此功能 |
|| 默认值 | 注释 |
|---------------------------|-----------------------|-----------------------------------------------------------------------------------------------------|
| `ws_host` | `ws://127.0.0.1:5800` | 正向WebSocket连接地址 |
| `reversed_ws_port` | `-1` | 反向WebSocket服务器端口,`-1` 为不开启反向WS。开启反向WS将会禁用正向WS连接 |
| `token` | | 鉴权令牌,留空则不进行鉴权 |
| `no_platform` | `false` | 是否禁止 Overflow 调用需要账号凭证的网络接口,如获取分享Key、群活跃等。在**非QQ平台****无法获取账号凭证**的平台 (如 Gensokyo) 使用 Overflow 请开启该选项 |
| `use_cq_code` | `false` | 发送消息时,是否使用 CQ 码格式 |
| `retry_times` | `5` | 正向WS断开连接后重连尝试次数,设为 `-1` 禁用自动重连 |
| `retry_wait_mills` | `5000` | 每次重连之间的间隔时间 (毫秒) |
| `retry_rest_mills` | `60000` | 重连次数用完后,等待多长时间 (毫秒) 后,重置次数再进行重连。设为 `-1` 禁用此功能 |
| `heartbeat_check_seconds` | `60` | WebSocket 心跳策略检查时间 (秒),设为 `0` 关闭心跳策略 |
8 changes: 6 additions & 2 deletions onebot/src/main/kotlin/client/config/BotConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ class BotConfig(
/**
* 重连间隔 (毫秒)
*/
val retryWaitMills: Long = 5000L,
val retryWaitMills: Long = 5_000L,
/**
* 重连休息时间 (毫秒)
*/
val retryRestMills: Long = 60000L,
val retryRestMills: Long = 60_000L,
/**
* 心跳包检测时间 (秒),设为0关闭检测
*/
val heartbeatCheckSeconds: Int = 60,
val parentJob: Job? = null
) {
val isInReverseMode get() = reversedPort in 1..65535
Expand Down
4 changes: 4 additions & 0 deletions onebot/src/main/kotlin/client/connection/WSClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class WSClient(
private var retryCount = 0
private var scheduleClose = false

init {
connectionLostTimeout = Math.max(0, config.heartbeatCheckSeconds)
}

@OptIn(InternalCoroutinesApi::class)
private val connectDef = CompletableDeferred<Boolean>(config.parentJob).apply {
invokeOnCompletion(
Expand Down
1 change: 1 addition & 0 deletions onebot/src/main/kotlin/client/connection/WSServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class WSServer(
val closeHandler = mutableListOf<() -> Unit>()

init {
connectionLostTimeout = Math.max(0, config.heartbeatCheckSeconds)
//等待测试
setWebSocketFactory(object : WebSocketServerFactory by DefaultWebSocketServerFactory() {
override fun close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public class BotBuilder private constructor(
private var reversedPort: Int = -1,
private var token: String = "",
private var retryTimes: Int = 5,
private var retryWaitMills: Long = 5000L,
private var retryRestMills: Long = 60000L,
private var retryWaitMills: Long = 5_000L,
private var retryRestMills: Long = 6_0000L,
private var heartbeatCheckSeconds: Int = 60,
private var printInfo: Boolean = true,
private var noPlatform: Boolean = false,
private var useCQCode: Boolean = false,
Expand Down Expand Up @@ -155,6 +156,7 @@ public class BotBuilder private constructor(
retryTimes = retryTimes,
retryWaitMills = retryWaitMills,
retryRestMills = retryRestMills,
heartbeatCheckSeconds = heartbeatCheckSeconds,
printInfo = printInfo,
noPlatform = noPlatform,
useCQCode = useCQCode,
Expand Down Expand Up @@ -191,6 +193,7 @@ interface IBotStarter {
retryTimes: Int,
retryWaitMills: Long,
retryRestMills: Long,
heartbeatCheckSeconds: Int,
printInfo: Boolean,
noPlatform: Boolean,
useCQCode: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ data class Config(
var retryWaitMills: Long = 5_000L,
@SerialName("retry_rest_mills")
var retryRestMills: Long = 60_000L,
@SerialName("heartbeat_check_seconds")
var heartbeatCheckSeconds: Int = 60,
)
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {
retryTimes = config.retryTimes,
retryWaitMills = config.retryWaitMills,
retryRestMills = config.retryRestMills,
heartbeatCheckSeconds = config.heartbeatCheckSeconds,
parentJob = job ?: defaultJob,
),
printInfo = printInfo,
Expand Down Expand Up @@ -290,6 +291,7 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {
retryTimes: Int,
retryWaitMills: Long,
retryRestMills: Long,
heartbeatCheckSeconds: Int,
printInfo: Boolean,
noPlatform: Boolean,
useCQCode: Boolean,
Expand All @@ -308,6 +310,7 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {
retryTimes = retryTimes,
retryWaitMills = retryWaitMills,
retryRestMills = retryRestMills,
heartbeatCheckSeconds = heartbeatCheckSeconds,
parentJob = parentJob
)
return if (logger != null) {
Expand Down

0 comments on commit ab225b4

Please sign in to comment.