fix(broker): update default broker settings and improve connection error handling

This commit is contained in:
Kingkor Roy Tirtho 2026-10-30 11:50:27 +06:00
parent 14bc279653
commit 2aaa0db81e
3 changed files with 16 additions and 5 deletions

View File

@ -553,6 +553,7 @@
<string name="jam_error_broker_host_empty">Broker host is empty</string> <string name="jam_error_broker_host_empty">Broker host is empty</string>
<string name="jam_error_connection_failed">Connection failed</string> <string name="jam_error_connection_failed">Connection failed</string>
<string name="jam_error_connection_lost">Connection lost</string> <string name="jam_error_connection_lost">Connection lost</string>
<string name="jam_error_broker_unreachable">Couldn't reach %1$s:%2$d. Check your network or pick another broker in Settings → Jam.</string>
<!-- Settings: Group Jam broker --> <!-- Settings: Group Jam broker -->
<string name="settings_jam_broker_content_description">Broker</string> <string name="settings_jam_broker_content_description">Broker</string>

View File

@ -33,12 +33,14 @@ import org.spotube.spotube.modules.settings.JamBroker
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -47,6 +49,7 @@ import kotlinx.serialization.json.Json
import org.jetbrains.compose.resources.getString import org.jetbrains.compose.resources.getString
import spotube.composeapp.generated.resources.Res import spotube.composeapp.generated.resources.Res
import spotube.composeapp.generated.resources.jam_error_broker_host_empty import spotube.composeapp.generated.resources.jam_error_broker_host_empty
import spotube.composeapp.generated.resources.jam_error_broker_unreachable
import spotube.composeapp.generated.resources.jam_error_connection_failed import spotube.composeapp.generated.resources.jam_error_connection_failed
import spotube.composeapp.generated.resources.jam_error_connection_lost import spotube.composeapp.generated.resources.jam_error_connection_lost
import spotube.composeapp.generated.resources.jam_error_no_broker import spotube.composeapp.generated.resources.jam_error_no_broker
@ -135,8 +138,15 @@ class JamRoomClient : KoinComponent {
val mqtt = startClient(broker, clientId) val mqtt = startClient(broker, clientId)
client = mqtt client = mqtt
try {
withTimeout(broker.connectionTimeoutSeconds.seconds) { withTimeout(broker.connectionTimeoutSeconds.seconds) {
mqtt.awaitConnectivity() // connectionState replays its current value, unlike awaitConnectivity's
// broadcast flow, which loses a CONNACK that lands before it is collected.
mqtt.connectionState.first { it is ConnectionState.Connected }
}
} catch (e: TimeoutCancellationException) {
throw IllegalStateException(
getString(Res.string.jam_error_broker_unreachable, broker.host, broker.port), e)
} }
_connectionError.value = null _connectionError.value = null
log.i { "Connected to ${broker.host}:${broker.port} room=$code as $clientId" } log.i { "Connected to ${broker.host}:${broker.port} room=$code as $clientId" }

View File

@ -98,9 +98,9 @@ data class UserSettings(
@Serializable @Serializable
data class JamBroker( data class JamBroker(
val name: String = "", val name: String = "",
val host: String = "test.mosquitto.org", val host: String = "broker.hivemq.com",
val port: Int = 1883, val port: Int = 8883,
val useTls: Boolean = false, val useTls: Boolean = true,
val username: String? = null, val username: String? = null,
val password: String? = null, val password: String? = null,
val clientIdPrefix: String = "spotube", val clientIdPrefix: String = "spotube",