Skip to content

Commit 8bacece

Browse files
authored
Merge pull request #14 from NieiR/master
wsClient reConnect
2 parents d592f70 + 60df370 commit 8bacece

File tree

50 files changed

+15742
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+15742
-17
lines changed

src/main/kotlin/net/lz1998/mirai/entity/WebSocketBotClient.kt

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import net.lz1998.mirai.alias.BFrameType
99
import net.lz1998.mirai.ext.*
1010
import net.lz1998.mirai.service.MyLoginSolver
1111
import net.lz1998.mirai.utils.*
12-
import net.lz1998.mirai.utils.toFrame
1312
import net.mamoe.mirai.Bot
1413
import net.mamoe.mirai.alsoLogin
1514
import net.mamoe.mirai.event.events.BotEvent
@@ -21,14 +20,16 @@ import net.mamoe.mirai.message.MessageEvent
2120
import okhttp3.*
2221
import okio.ByteString
2322
import okio.ByteString.Companion.toByteString
23+
import java.lang.Thread.sleep
2424
import java.util.concurrent.TimeUnit
2525

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

2929

30-
private var lastWsConnectTime: Long = 0
31-
private lateinit var wsClient: WebSocket
30+
// private var lastWsConnectTime: Long = 0
31+
32+
private var wsClient: WebSocket? = null
3233
private var httpClient: OkHttpClient = OkHttpClient.Builder()
3334
.callTimeout(20, TimeUnit.SECONDS)
3435
.connectTimeout(20, TimeUnit.SECONDS)
@@ -47,11 +48,10 @@ class WebsocketBotClient(override var botId: Long, override var password: String
4748
GlobalScope.launch {
4849
val req = withContext(Dispatchers.IO) { BFrame.parseFrom(bytes.toByteArray()) }
4950
val resp = onRemoteApi(req)
50-
val ok = wsClient.send(resp.toByteArray().toByteString())
51-
if (!ok) {
51+
val ok = wsClient?.send(resp.toByteArray().toByteString())
52+
if (ok==null || !ok) {
5253
wsConnect()
5354
}
54-
5555
}
5656
super.onMessage(webSocket, bytes)
5757
}
@@ -63,36 +63,46 @@ class WebsocketBotClient(override var botId: Long, override var password: String
6363

6464
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
6565
println("websocket 已关闭")
66+
wsClient = null
6667
super.onClosed(webSocket, code, reason)
6768
}
6869

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

7476
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
7577
println("websocket失败${t.message}")
76-
t.printStackTrace()
78+
wsClient = null
79+
// t.printStackTrace()
7780
wsConnect()
7881
super.onFailure(webSocket, t, response)
7982
}
8083
}
8184

8285

83-
@Synchronized
86+
// @Synchronized
8487
fun wsConnect() {
85-
val now = System.currentTimeMillis()
86-
if (now - lastWsConnectTime > 5000L) {
88+
if (wsClient == null) {
8789
println("ws try connect")
88-
wsClient = httpClient.newWebSocket(wsRequest, wsListener)
89-
lastWsConnectTime = now
90+
synchronized(this) {
91+
wsClient = httpClient.newWebSocket(wsRequest, wsListener)
92+
}
9093
} else {
91-
println("wait ws reconnect interval 5s")
94+
return
9295
}
96+
sleep(5000)
97+
// val now = System.currentTimeMillis()
98+
// if (now - lastWsConnectTime > 5000L) {
99+
100+
// lastWsConnectTime = now
101+
// } else {
102+
// println("wait ws reconnect interval 5s")
103+
// }
93104
}
94105

95-
96106
override suspend fun initBot() {
97107
wsClient = httpClient.newWebSocket(wsRequest, wsListener)
98108
bot = Bot(botId, password) {
@@ -156,10 +166,10 @@ class WebsocketBotClient(override var botId: Long, override var password: String
156166
override suspend fun onBotEvent(botEvent: BotEvent) {
157167
val eventFrame = botEvent.toFrame() ?: return
158168
// TODO 写二进制还是json?配置
159-
val ok = wsClient.send(eventFrame.toByteArray().toByteString())
160-
if (!ok) {
169+
val ok = wsClient?.send(eventFrame.toByteArray().toByteString())
170+
if (ok==null || !ok) {
161171
wsConnect()
162172
}
163173
}
164174

165-
}
175+
}

src/main/resources/static/css/chunk-249701d4.3fb7e631.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/resources/static/css/chunk-vendors.47b5330a.css

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/resources/static/favicon.ico

4.19 KB
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.

0 commit comments

Comments
 (0)