Skip to content

Commit

Permalink
use readable close frame code
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Jan 18, 2024
1 parent 1f4d550 commit 0684f42
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,34 @@ class ConnectFactory private constructor(
return ConnectFactory(config, logger)
}
}
}
enum class CloseCode(val code: Int) {
NORMAL(1000),
GOING_AWAY(1001),
PROTOCOL_ERROR(1002),
REFUSE(1003),
NO_CODE(1005),
ABNORMAL_CLOSE(1006),
NO_UTF8(1007),
POLICY_VALIDATION(1008),
TOO_BIG(1009),
EXTENSION(1010),
UNEXPECTED_CONDITION(1011),
SERVICE_RESTART(1012),
TRY_AGAIN_LATER(1013),
BAD_GATEWAY(1014),
TLS_ERROR(1015),
NEVER_CONNECTED(-1),
BUGGY_CLOSE(-2),
FLASH_POLICY(-3);

override fun toString(): String {
return name.uppercase()
}

companion object {
fun valueOf(code: Int): CloseCode? {
return values().firstOrNull { it.code == code }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ class WSClient(
}

override fun onClose(code: Int, reason: String, remote: Boolean) {
logger.info("▌ 服务器连接因 {} 已关闭 ({})", reason, code)
logger.info(
"▌ 服务器连接因 {} 已关闭 (关闭码: {})",
reason.ifEmpty { "未知原因" },
CloseCode.valueOf(code) ?: code
)
runCatching {
if (mutex.isLocked) mutex.unlock()
if (ActionSendUtils.mutex.isLocked) ActionSendUtils.mutex.unlock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ class WSServer(
}

override fun onClose(conn: WebSocket, code: Int, reason: String, remote: Boolean) {
logger.info("▌ 反向 WebSocket 客户端连接因 {} 已关闭 ({})", reason, code)
logger.info(
"▌ 反向 WebSocket 客户端连接因 {} 已关闭 (关闭码: {})",
reason.ifEmpty { "未知原因" },
CloseCode.valueOf(code) ?: code
)
runCatching {
if (mutex.isLocked) mutex.unlock()
if (ActionSendUtils.mutex.isLocked) ActionSendUtils.mutex.unlock()
Expand Down

0 comments on commit 0684f42

Please sign in to comment.