diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketClientService.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketClientService.kt index 7c89331..8e3c428 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketClientService.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/WebSocketClientService.kt @@ -2,19 +2,12 @@ package moe.fuqiuluo.shamrock.remote.service -import com.tencent.qqnt.kernel.nativeinterface.MsgElement -import com.tencent.qqnt.kernel.nativeinterface.MsgRecord import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job import kotlinx.coroutines.launch import moe.fuqiuluo.shamrock.remote.service.api.WebSocketClientServlet -import moe.fuqiuluo.shamrock.remote.service.config.ShamrockConfig import moe.fuqiuluo.shamrock.remote.service.data.push.* -import moe.fuqiuluo.shamrock.tools.json -import moe.fuqiuluo.qqinterface.servlet.GroupSvc -import moe.fuqiuluo.qqinterface.servlet.TicketSvc -import moe.fuqiuluo.qqinterface.servlet.msg.convert.toSegments import moe.fuqiuluo.shamrock.helper.Level import moe.fuqiuluo.shamrock.helper.LogCenter import moe.fuqiuluo.shamrock.remote.service.api.GlobalEventTransmitter diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketClientServlet.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketClientServlet.kt index 34580ed..edcc15b 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketClientServlet.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketClientServlet.kt @@ -7,6 +7,8 @@ import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.Job import kotlinx.coroutines.launch +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import moe.fuqiuluo.shamrock.remote.action.ActionManager @@ -36,6 +38,8 @@ internal abstract class WebSocketClientServlet( url: String, wsHeaders: Map ) : BaseTransmitServlet, WebSocketClient(URI(url), wsHeaders) { + private val sendLock = Mutex() + override fun allowTransmit(): Boolean { return ShamrockConfig.openWebSocketClient() } @@ -89,10 +93,12 @@ internal abstract class WebSocketClientServlet( cancelFlowJobs() } - protected inline fun pushTo(body: T) { + protected suspend inline fun pushTo(body: T) { if (!allowTransmit() || isClosed || isClosing) return try { - send(GlobalJson.encodeToString(body)) + sendLock.withLock { + send(GlobalJson.encodeToString(body)) + } } catch (e: Throwable) { LogCenter.log("被动WS推送失败: ${e.stackTraceToString()}", Level.ERROR) } diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketTransmitServlet.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketTransmitServlet.kt index 1913e4e..44c0ffb 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketTransmitServlet.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/remote/service/api/WebSocketTransmitServlet.kt @@ -5,6 +5,8 @@ package moe.fuqiuluo.shamrock.remote.service.api import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import moe.fuqiuluo.shamrock.remote.action.ActionManager @@ -35,6 +37,7 @@ internal abstract class WebSocketTransmitServlet( host:String, port: Int ) : BaseTransmitServlet, WebSocketServer(InetSocketAddress(host, port)) { + private val sendLock = Mutex() protected val eventReceivers: MutableList = Collections.synchronizedList(mutableListOf()) override val address: String @@ -113,10 +116,12 @@ internal abstract class WebSocketTransmitServlet( initTransmitter() } - protected inline fun pushTo(body: T) { + protected suspend inline fun pushTo(body: T) { if(!allowTransmit()) return try { - broadcastTextEvent(GlobalJson.encodeToString(body)) + sendLock.withLock { + broadcastTextEvent(GlobalJson.encodeToString(body)) + } } catch (e: Throwable) { LogCenter.log("WS推送失败: ${e.stackTraceToString()}", Level.ERROR) } diff --git a/xposed/src/main/java/moe/fuqiuluo/shamrock/xposed/actions/InitRemoteService.kt b/xposed/src/main/java/moe/fuqiuluo/shamrock/xposed/actions/InitRemoteService.kt index 7dfcdc7..c16a2c7 100644 --- a/xposed/src/main/java/moe/fuqiuluo/shamrock/xposed/actions/InitRemoteService.kt +++ b/xposed/src/main/java/moe/fuqiuluo/shamrock/xposed/actions/InitRemoteService.kt @@ -92,13 +92,11 @@ internal class InitRemoteService : IAction { GlobalScope.launch { try { if (url.startsWith("ws://") || url.startsWith("wss://")) { - var wsClient = WebSocketClientService(url, wsHeaders) + val wsClient = WebSocketClientService(url, wsHeaders) wsClient.connect() timer(initialDelay = 5000L, period = 5000L) { if (wsClient.isClosed || wsClient.isClosing) { - wsClient.cancelFlowJobs() - wsClient = WebSocketClientService(url, wsHeaders) - wsClient.connect() + wsClient.reconnect() } } } else {