Skip to content

Commit 0e12895

Browse files
committed
将ws的创建和销毁都放在同一主线程中执行
1 parent 59a7308 commit 0e12895

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

sdk/explorer-link-android/src/main/java/com/tencent/iot/explorer/link/core/auth/socket/WSClientManager.kt

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import java.util.*
2222
internal class WSClientManager private constructor() {
2323

2424
private var TAG = WSClientManager.javaClass.simpleName
25+
private val scope = CoroutineScope(Dispatchers.IO)
2526
private var hasListener = false
2627
private var job: Job? = null
2728
private lateinit var heartJob: Job
@@ -185,21 +186,23 @@ internal class WSClientManager private constructor() {
185186
*/
186187
@Synchronized
187188
private fun createSocketClient() {
188-
val myHost = if (debugTag.isNotEmpty())
189-
host + debugTag
190-
else
191-
host
192-
//创建WebSocket
193-
client = JWebSocketClient(URI(myHost), handler, connectListener)
194-
client!!.connectionLostTimeout = 0
195-
client!!.connect()
189+
scope.launch(Dispatchers.Main) {
190+
val myHost = if (debugTag.isNotEmpty())
191+
host + debugTag
192+
else
193+
host
194+
//创建WebSocket
195+
client = JWebSocketClient(URI(myHost), handler, connectListener)
196+
client?.connectionLostTimeout = 0
197+
client?.connect()
198+
}
196199
}
197200

198201
/**
199202
* 发送心跳包
200203
*/
201204
private fun startHeartJob() {
202-
heartJob = CoroutineScope(Dispatchers.IO).launch {
205+
heartJob = scope.launch(Dispatchers.IO) {
203206
while (isKeep) {
204207
sendMessage(param)
205208
if (heartMessageList.isNotEmpty() && heartCount == 5) {
@@ -227,13 +230,15 @@ internal class WSClientManager private constructor() {
227230
hasListener = true
228231
L.e("开始重连")
229232
if (job == null) {
230-
job = CoroutineScope(Dispatchers.IO).launch {
233+
job = scope.launch(Dispatchers.IO) {
231234
while (hasListener) {
232235
try {
233236
if (WifiUtil.ping("www.baidu.com") || WifiUtil.ping("iot.cloud.tencent.com")) {
234237
if (client != null) {
235-
client?.destroy()
236-
client = null
238+
scope.launch(Dispatchers.Main) {
239+
client?.destroy()
240+
client = null
241+
}
237242
}
238243
createSocketClient()
239244
L.d("正在尝试重新连接wss://iot.cloud.tencent.com")
@@ -279,11 +284,16 @@ internal class WSClientManager private constructor() {
279284
}
280285

281286
override fun disconnected() {
282-
L.d("连接断开")
283-
client?.destroy()
284-
client = null
285-
startJob()
286-
socketCallback?.disconnected()
287+
val destroyJob = scope.launch(Dispatchers.Main) {
288+
L.d("连接断开")
289+
client?.destroy()
290+
client = null
291+
}
292+
scope.launch {
293+
destroyJob.join()
294+
socketCallback?.disconnected()
295+
startJob()
296+
}
287297
}
288298

289299
override fun onOpen() {
@@ -436,12 +446,14 @@ internal class WSClientManager private constructor() {
436446
* 销毁
437447
*/
438448
fun destroy() {
439-
hasListener = false
440-
isKeep = false
441-
client?.destroy()
442-
client = null
443-
stopHeartJob()
444-
stopJob()
449+
scope.launch(Dispatchers.Main) {
450+
hasListener = false
451+
isKeep = false
452+
client?.destroy()
453+
client = null
454+
stopHeartJob()
455+
stopJob()
456+
}
445457
}
446458

447459
}

0 commit comments

Comments
 (0)