I have corrected the code (Thanks to @Larme) to solve the problem.
var state = mqtt.connect()
var subscribed = false
autoreleasepool {
let runLoop = RunLoop.current
while runLoop.run(mode: .default, before: Date.distantFuture) {
if mqtt.connState == .connected && !subscribed {
mqtt.subscribe("test/#")
subscribed = true
}
}
Before subscribing topics, it is important to wait until the connection is made. If the subscription is made immediately after the connect on a SSL connection, the broker will log an error:
2025-06-02T10:59:43: Client CocoaMQTT-88249 closed its connection.
2025-06-02T11:03:37: Client connection from 93.208.209.34 failed: error:0A00010B:SSL routines::wrong version number.
The above (resulting) error message might point to the wrong direction.