Open yusufonderd opened 6 years ago
The connection was closed by yourself or the broker before the SUBACK packet was received.
Which broker do you use? Can you subscribe with QoS Exactly Once for the topic you use with another client?
@yusufonderd What is your subscriptionTopic
here? Does it include device-id
?
@mendirattanishant My subscriptionTopic is custom string. It starts with 'indoor/position/..'
@yusufonderd and it works fine in Android client? In my case it was not connecting as I was not setting the clientId
correctly.
Yes it is worked fine android side. I was set clientId normally.
let clientID = "Indoor" + uniqueValue
let transport = MQTTWebsocketTransport()
transport.url = websocketUrl
self.session = MQTTSession(clientId: clientID)
self.session.transport = transport
self.session.delegate = self
self.session.connectAndWaitTimeout(5.0)
self.session.connect(connectHandler: { (err) in
if let error = err{
print("MQTTx err:\(error)")
}else{
print("MQTTx successfully connected")
session?.subscribe(toTopic: subscriptionTopic, at: MQTTQosLevel.exactlyOnce)
}
})
@yusufonderd can you just call connect()
method on session and then have your delegates method subscribe to topic.
Once there is a connection established, the delegate method
func handleEvent(_ session: MQTTSession!, event eventCode: MQTTSessionEvent, error: Error!)
would get called. In the delegate method, call subscribe()
to subscribe to the topics.
func handleEvent(_ session: MQTTSession!, event eventCode: MQTTSessionEvent, error: Error!) {
switch eventCode {
case .connected:
sessionConnected = true
session.subscribe(toTopic: subscriptionTopic, at: MQTTQosLevel.exactlyOnce)
case .connectionClosed:
sessionConnected = false
default:
sessionError = true
}
}
The delegate method will give you a better clarity if the connection is getting established or not.
Change your MQTTQosLevel to .atMostOnce
.
@yusufonderd is your issue resolved?
Unfortunately I can’t resolved. Issue still exists.
When I successfully connected socket. I subscribe topic. Then I'm getting error and web socket closed.
I'm getting this error :