novastone-media / MQTT-Client-Framework

iOS, macOS, tvOS native ObjectiveC MQTT Client Framework
Other
1.84k stars 466 forks source link

Cannot subscribe topic #466

Open yusufonderd opened 6 years ago

yusufonderd commented 6 years ago

When I successfully connected socket. I subscribe topic. Then I'm getting error and web socket closed.

 session?.subscribe(toTopic: subscriptionTopic, at: MQTTQosLevel.exactlyOnce, subscribeHandler: { (err, gQoss) in
    if (err != nil){
       print("MQTTSessionx Subscription failed \(err)");
      }else{
        print("MQTTSessionx Subscription sucessfull! Granted Qos: \(gQoss)");
      }
 })

I'm getting this error :

MQTTSessionx Subscription failed Optional(Error Domain=MQTT Code=-6 "No response" UserInfo={NSLocalizedDescription=No response}) [SR] NSStreamEventHasSpaceAvailable <NSCFOutputStream: 0x108118480> [MQTTSession] mqttTransport send [MQTTWebsocketTransport] send(67):<82410002 003c696e 646f6f72 2f706f73 6974696f 6e732f6c 61746573 742f6265 35386131 31622d31 3236652d 34373639 2d386538 652d6438 30623830 39353462 623602> [SR] NSStreamEventOpenCompleted <NSCFOutputStream: 0x108045080> [SR] Closing with code 1000 reason (null) [SR] Trying to disconnect [SR] NSStreamEventHasSpaceAvailable <__NSCFOutputStream: 0x108045080>

ckrey commented 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?

mendirattanishant commented 6 years ago

@yusufonderd What is your subscriptionTopic here? Does it include device-id?

yusufonderd commented 6 years ago

@mendirattanishant My subscriptionTopic is custom string. It starts with 'indoor/position/..'

mendirattanishant commented 6 years ago

@yusufonderd and it works fine in Android client? In my case it was not connecting as I was not setting the clientId correctly.

yusufonderd commented 6 years ago

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)
       }
    })
mendirattanishant commented 6 years ago

@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
        }
    }

Reference: https://github.com/novastone-media/MQTT-Client-Framework/blob/master/MQTTSwift/MQTTSwift/MQTTSwift.swift

The delegate method will give you a better clarity if the connection is getting established or not.

Adrxx commented 6 years ago

Change your MQTTQosLevel to .atMostOnce.

jcavar commented 6 years ago

@yusufonderd is your issue resolved?

yusufonderd commented 6 years ago

Unfortunately I can’t resolved. Issue still exists.