From 3ac19def63116ef0dc0be966e912a32ede2b72f2 Mon Sep 17 00:00:00 2001 From: Pete Gautier Date: Thu, 1 Feb 2024 11:40:10 -0800 Subject: [PATCH] add resubscribe --- .../com/example/moderatedchat/MainActivity.kt | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/example/moderatedchat/MainActivity.kt b/app/src/main/java/com/example/moderatedchat/MainActivity.kt index 8643482..322c3e2 100644 --- a/app/src/main/java/com/example/moderatedchat/MainActivity.kt +++ b/app/src/main/java/com/example/moderatedchat/MainActivity.kt @@ -42,6 +42,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import kotlinx.coroutines.yield @@ -173,7 +174,6 @@ fun ModeratedChatLayout( withContext(Dispatchers.IO) { coroutineScope { launch { - // TODO: Ok, so how do I reliably resubscribe after my token expires? val tokenExpiresInSecs = tokenExpiresAt - (System.currentTimeMillis() / 1000) println("token expires in $tokenExpiresInSecs") if (topicClient == null || tokenExpiresInSecs < 10) { @@ -199,14 +199,22 @@ fun ModeratedChatLayout( println("cancelling existing subscribe job") subscribeJob!!.cancelAndJoin() } - subscribeJob = launch { - topicSubscribe(language = currentLanguage) - { - val jsonMessage = JSONObject(it) - val parsedMessage = parseMessage(jsonMessage) - currentMessages.add(parsedMessage) - println("message added to current messages list") + while (true) { + subscribeJob = launch { + topicSubscribe(language = currentLanguage) + { + val jsonMessage = JSONObject(it) + val parsedMessage = parseMessage(jsonMessage) + currentMessages.add(parsedMessage) + println("message added to current messages list") + } } + val resubscribeAfterSecs = 180L + delay(resubscribeAfterSecs * 1000) + subscribeJob?.cancelAndJoin() + topicClient?.close() + getTopicClient(userName, userId) + print("resubscribing") } } } @@ -340,12 +348,11 @@ fun LanguageDropdown( } } - suspend fun topicSubscribe( language: String, onMessage: (String) -> Unit ) { - println("Subscribing to chat-$language") + println("Subscribing to chat-$language with $topicClient") when (val response = topicClient!!.subscribe("moderator", "chat-$language")) { is TopicSubscribeResponse.Subscription -> coroutineScope { val subscribeBeginSecs = System.currentTimeMillis() / 1000