[안드로이드 스튜디오 코틀린] Kotlin MQTT 구동시 응답이 오지 않을 때(Client is not connected (32104))
MQTT를 생성하여 Topic을 구독하는 도중,
응답값이 잘 오지 않았습니다.
Client is not connected (32104) 오류가 계속 뜨거나 , Fail은 없고 subscribe callback이 오지 않았습니다.
해결 방법은
waitForCompletion()
함수를 사용해줍니다.
코드에서는 이렇게 연결해주면 됩니다.
if (client?.isConnected == false) {
val token = client?.connect(mqttConnectOptions,"", MqttConnectListener())
token?.waitForCompletion()
}
- client : MqttAsyncClient
- mqttConnectOptions : MqttConnectOptions()
문서를 확인해보면
-> 토큰과 연결된 작업이 완료될 때까지 스레드를 차단하는 함수입니다.
아마 여러개의 스레드를 사용하여 구동하는 앱일 경우에 발견할 수 있는 에러인것 같습니다.
아래 스택오버플로우도 참고하세요
https://stackoverflow.com/questions/58360371/mqttasyncclient-client-is-not-connected-32104
MqttAsyncClient Client is not connected (32104)
I'm using a MqttAsyncClient in Java. The following code is only a part of how i'm using it, the problem becomes when i'm trying to subscribe to any topic i get the error: Client is not connected (
stackoverflow.com