Skip to content

Commit

Permalink
Merge pull request #14 from NieiR/master
Browse files Browse the repository at this point in the history
wsClient reConnect
  • Loading branch information
lz1998 committed Oct 27, 2020
2 parents d592f70 + 60df370 commit 8bacece
Show file tree
Hide file tree
Showing 50 changed files with 15,742 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/main/kotlin/net/lz1998/mirai/entity/WebSocketBotClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import net.lz1998.mirai.alias.BFrameType
import net.lz1998.mirai.ext.*
import net.lz1998.mirai.service.MyLoginSolver
import net.lz1998.mirai.utils.*
import net.lz1998.mirai.utils.toFrame
import net.mamoe.mirai.Bot
import net.mamoe.mirai.alsoLogin
import net.mamoe.mirai.event.events.BotEvent
Expand All @@ -21,14 +20,16 @@ import net.mamoe.mirai.message.MessageEvent
import okhttp3.*
import okio.ByteString
import okio.ByteString.Companion.toByteString
import java.lang.Thread.sleep
import java.util.concurrent.TimeUnit

class WebsocketBotClient(override var botId: Long, override var password: String, wsUrl: String) : RemoteBot {
override lateinit var bot: Bot


private var lastWsConnectTime: Long = 0
private lateinit var wsClient: WebSocket
// private var lastWsConnectTime: Long = 0

private var wsClient: WebSocket? = null
private var httpClient: OkHttpClient = OkHttpClient.Builder()
.callTimeout(20, TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS)
Expand All @@ -47,11 +48,10 @@ class WebsocketBotClient(override var botId: Long, override var password: String
GlobalScope.launch {
val req = withContext(Dispatchers.IO) { BFrame.parseFrom(bytes.toByteArray()) }
val resp = onRemoteApi(req)
val ok = wsClient.send(resp.toByteArray().toByteString())
if (!ok) {
val ok = wsClient?.send(resp.toByteArray().toByteString())
if (ok==null || !ok) {
wsConnect()
}

}
super.onMessage(webSocket, bytes)
}
Expand All @@ -63,36 +63,46 @@ class WebsocketBotClient(override var botId: Long, override var password: String

override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
println("websocket 已关闭")
wsClient = null
super.onClosed(webSocket, code, reason)
}

override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
println("websocket正在关闭 $reason")
wsClient = null
super.onClosing(webSocket, code, reason)
}

override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
println("websocket失败${t.message}")
t.printStackTrace()
wsClient = null
// t.printStackTrace()
wsConnect()
super.onFailure(webSocket, t, response)
}
}


@Synchronized
// @Synchronized
fun wsConnect() {
val now = System.currentTimeMillis()
if (now - lastWsConnectTime > 5000L) {
if (wsClient == null) {
println("ws try connect")
wsClient = httpClient.newWebSocket(wsRequest, wsListener)
lastWsConnectTime = now
synchronized(this) {
wsClient = httpClient.newWebSocket(wsRequest, wsListener)
}
} else {
println("wait ws reconnect interval 5s")
return
}
sleep(5000)
// val now = System.currentTimeMillis()
// if (now - lastWsConnectTime > 5000L) {

// lastWsConnectTime = now
// } else {
// println("wait ws reconnect interval 5s")
// }
}


override suspend fun initBot() {
wsClient = httpClient.newWebSocket(wsRequest, wsListener)
bot = Bot(botId, password) {
Expand Down Expand Up @@ -156,10 +166,10 @@ class WebsocketBotClient(override var botId: Long, override var password: String
override suspend fun onBotEvent(botEvent: BotEvent) {
val eventFrame = botEvent.toFrame() ?: return
// TODO 写二进制还是json?配置
val ok = wsClient.send(eventFrame.toByteArray().toByteString())
if (!ok) {
val ok = wsClient?.send(eventFrame.toByteArray().toByteString())
if (ok==null || !ok) {
wsConnect()
}
}

}
}
1 change: 1 addition & 0 deletions src/main/resources/static/css/chunk-249701d4.3fb7e631.css

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/main/resources/static/css/chunk-vendors.47b5330a.css

Large diffs are not rendered by default.

Binary file added src/main/resources/static/favicon.ico
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3,638 changes: 3,638 additions & 0 deletions src/main/resources/static/img/fa-brands-400.ccfdb9dc.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
806 changes: 806 additions & 0 deletions src/main/resources/static/img/fa-regular-400.e75dfd90.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5,016 changes: 5,016 additions & 0 deletions src/main/resources/static/img/fa-solid-900.03ba7cb7.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/main/resources/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang=zh-Hans>
<head>
<meta charset=utf-8>
<meta content="IE=edge" http-equiv=X-UA-Compatible>
<meta content="width=device-width,initial-scale=1" name=viewport>
<link href=/favicon.ico rel=icon>
<title>spring_mirai_client_ui</title>
<link href=/css/chunk-249701d4.3fb7e631.css rel=prefetch>
<link href=/js/chunk-249701d4.1a96f03f.js rel=prefetch>
<link as=style href=/css/chunk-vendors.47b5330a.css rel=preload>
<link as=script href=/js/app.2913b2e7.js rel=preload>
<link as=script href=/js/chunk-vendors.e67a52b7.js rel=preload>
<link href=/css/chunk-vendors.47b5330a.css rel=stylesheet>
</head>
<body>
<noscript><strong>We're sorry but spring_mirai_client_ui doesn't work properly without JavaScript enabled. Please enable
it to continue.</strong></noscript>
<div id=app></div>
<script src=/js/chunk-vendors.e67a52b7.js></script>
<script src=/js/app.2913b2e7.js></script>
</body>
</html>
2 changes: 2 additions & 0 deletions src/main/resources/static/js/app.2913b2e7.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

180 changes: 180 additions & 0 deletions src/main/resources/static/js/app.2913b2e7.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/main/resources/static/js/chunk-249701d4.1a96f03f.js

Large diffs are not rendered by default.

Loading

0 comments on commit 8bacece

Please sign in to comment.