Skip to content

Commit c03c18e

Browse files
committed
feat(client):优化客户端连接与心跳检测机制- 在BaseClient初始化时更新最后一次心跳时间
- 将ClientManager中的HeartbeatTimeOut改为常量- 调整心跳检测间隔从15秒到20秒 - 增加对hashKey为空的连接拒绝处理- 延长握手超时检测时间至15秒- 优化WebSocket错误处理逻辑- 简化handleTextMessage中的body获取逻辑
1 parent 5fea2c9 commit c03c18e

File tree

5 files changed

+20
-18
lines changed

5 files changed

+20
-18
lines changed

src/main/kotlin/WebsocketServer.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ object WebsocketServer {
8282

8383
// 添加10秒握手超时检测
8484
coroutineScope.launch {
85-
delay(10000)
85+
delay(15*1000)
8686
try {
8787
val serverPackage = ClientManager.getServerPackageBySession(session)
8888
val botClient = ClientManager.getBotClient()
@@ -116,6 +116,7 @@ object WebsocketServer {
116116
}
117117

118118
fun handleError(session: WebSocketSession, error: Exception) {
119+
coroutineScope.launch { session.close(CloseReason(CloseReason.Codes.INTERNAL_ERROR, "Error Message.")) }
119120
logger.error("[Websocket] 处理消息时发生错误", error)
120121
}
121122

@@ -194,18 +195,11 @@ object WebsocketServer {
194195
}
195196
}
196197

197-
private fun checkPackLegal(data: JSONObject){
198-
199-
}
200-
201198
fun handleTextMessage(session: ClientSession, payload: String) {
202199
try {
203200
val data = JSONObject.parseObject(payload)
204201
val header = data.getJSONObject("header")
205-
var body = data.getJSONObject("body")
206-
if(body == null){
207-
body = JSONObject()
208-
}
202+
val body = data.getJSONObject("body")?: JSONObject()
209203
val msgType = header.getString("type")
210204
val packId = header.getString("id")
211205

src/main/kotlin/client/BaseClient.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ abstract class BaseClient(session: ClientSession, clientType: ClientType) {
2626
init {
2727
mClientType = clientType
2828
mSession = session
29+
//连接进入时更新最后一次心跳时间
30+
updateLastHeartbeatTime()
2931
}
3032

3133
/**

src/main/kotlin/events/server/EventHandler/Server_handle_BindConfirm.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ object Server_handle_BindConfirm: BaseEvent() {
2424
}
2525
try {
2626
val config: JSONObject = getServerConfig(serverId, isMoreGroup)
27-
if(mClient == null) return@Consumer
27+
if(mClient == null) {
28+
return@Consumer
29+
}
2830
if (mClient!!.sendMessage(ServerSendEvent.ServerSendConfig, config, mPackId)) {
2931
val bindServerPack = JSONObject()
3032
bindServerPack["group"] = groupOpenid

src/main/kotlin/events/server/EventHandler/Server_handle_ShakeHand.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ object Server_handle_ShakeHand: BaseEvent() {
6262

6363
fun runShake(session: ClientSession): Boolean {
6464
val serverId: String? = mBody.getString("serverId")
65-
val hashKey: String = mBody.getString("hashKey")
65+
val hashKey: String? = mBody.getString("hashKey")
6666

67-
val platform: String = mBody.getString("platform")
68-
val name: String = mBody.getString("name")
69-
val version: String = mBody.getString("version")
67+
val platform: String = mBody.getString("platform")?: "Unknown"
68+
val name: String = mBody.getString("name")?: "Unknown"
69+
val version: String = mBody.getString("version")?: "0.0.0"
7070

7171
val serverClient = ServerClient(session)
7272

@@ -76,6 +76,12 @@ object Server_handle_ShakeHand: BaseEvent() {
7676
return false
7777
}
7878

79+
if (hashKey == null || hashKey.isEmpty()) {
80+
//拒绝连接
81+
serverClient.shutdown(CloseReason.Codes.VIOLATED_POLICY, "hashKey为空.")
82+
return false
83+
}
84+
7985
serverClient.mServerId = serverId
8086
serverClient.mHashKey = hashKey
8187
serverClient.platform = platform

src/main/kotlin/tools/manager/ClientManager.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ object ClientManager {
2222
//等待BotClient连接队列
2323
private val waitingBotClientList: MutableMap<String, ServerClient> = ConcurrentHashMap<String, ServerClient>()
2424
//心跳超时时间
25-
private val HeartbeatTimeOut: Long = 60*1000
25+
private const val HeartbeatTimeOut: Long = 60*1000
2626

2727
private var mBotClient: BotClient? = null
2828
//心跳检测协程
@@ -32,7 +32,7 @@ object ClientManager {
3232
// 每隔5秒检查一次心跳,替代原来的 scheduler.scheduleAtFixedRate
3333
coroutineScope.launch {
3434
while (isActive) {
35-
delay(15*1000) // 5秒延迟
35+
delay(20*1000)
3636
checkHeartbeats()
3737
}
3838
}
@@ -216,6 +216,4 @@ object ClientManager {
216216
false
217217
}
218218
}
219-
220-
221219
}

0 commit comments

Comments
 (0)