Closed aratalcordtech closed 4 years ago
2019-11-03 11:08:00.298856-0500 MindMics[439:74872] [MQTTSession] init Optional(<SecIdentityRef: 0x28368ccc0>) :::: <SecIdentityRef: 0x28368ccc0> Optional(<SecTrustRef: 0x2804bdf80>) :::: <SecTrustRef: 0x2804bdf80> 2019-11-03 11:08:00.396437-0500 MindMics[439:74872] [MQTTSession] connecting 2019-11-03 11:08:00.396594-0500 MindMics[439:74872] [MQTTCoreDataPersistence] deleteAllFlowsForClientId 439 2019-11-03 11:08:00.396965-0500 MindMics[439:74872] [MQTTPersistence] Persistent store: /var/mobile/Containers/Data/Application/19F6FACC-17C7-4205-BA98-9FD70C3A96F4/Documents/MQTTClient 2019-11-03 11:08:00.399568-0500 MindMics[439:74872] [MQTTCFSocketTransport] open 2019-11-03 11:08:00.400596-0500 MindMics[439:74872] [MQTTCFSocketEncoder] setState 0/0 faceup/Face down Orientation 2019-11-03 11:08:02.491280-0500 MindMics[439:74872] [MQTTCFSocketDecoder] NSStreamEventOpenCompleted 2019-11-03 11:08:02.491515-0500 MindMics[439:74872] [MQTTCFSocketEncoder] NSStreamEventOpenCompleted 2019-11-03 11:08:02.491660-0500 MindMics[439:74872] [MQTTCFSocketEncoder] NSStreamEventHasSpaceAvailable 2019-11-03 11:08:02.491803-0500 MindMics[439:74872] [MQTTCFSocketEncoder] setState 0/1 2019-11-03 11:08:02.491956-0500 MindMics[439:74872] [MQTTSession] mqttTransportDidOpen 2019-11-03 11:08:02.492085-0500 MindMics[439:74872] [MQTTSession] sending CONNECT 2019-11-03 11:08:02.492949-0500 MindMics[439:74872] [MQTTMessage] wireFormat(63)={length = 63, bytes = 0x103d0004 4d515454 04c6003c 00033433 ... 476e0004 74657374 }... 2019-11-03 11:08:02.494028-0500 MindMics[439:74872] [MQTTSession] mqttTransport send 2019-11-03 11:08:02.494381-0500 MindMics[439:74872] [MQTTCFSocketEncoder] buffer to write (63)={length = 63, bytes = 0x103d0004 4d515454 04c6003c 00033433 ... 476e0004 74657374 }... 2019-11-03 11:08:02.495560-0500 MindMics[439:74872] [MQTTCFSocketEncoder] NSStreamEventHasSpaceAvailable 2019-11-03 11:08:03.056985-0500 MindMics[439:74872] [MQTTCFSocketDecoder] NSStreamEventHasBytesAvailable 2019-11-03 11:08:03.057171-0500 MindMics[439:75043] [] nw_socket_handle_socket_event [C2.1:1] Socket SO_ERROR [54: Connection reset by peer] 2019-11-03 11:08:03.057425-0500 MindMics[439:74872] [MQTTCFSocketDecoder] received (0)={length = 0, bytes = 0x}... 2019-11-03 11:08:03.057546-0500 MindMics[439:74872] [MQTTSession] mqttTransport didReceiveMessage 2019-11-03 11:08:03.057722-0500 MindMics[439:74872] [MQTTDecoder] #streams=1 2019-11-03 11:08:03.058539-0500 MindMics[439:74872] [MQTTCFSocketDecoder] NSStreamEventEndEncountered 2019-11-03 11:08:03.059050-0500 MindMics[439:74872] [MQTTSession] mqttTransport mqttTransportDidClose MQTT connectionClosedByBroker 2019-11-03 11:08:03.059788-0500 MindMics[439:74872] [MQTTSession] closeInternal 2019-11-03 11:08:03.059937-0500 MindMics[439:74872] [MQTTCFSocketTransport] close MQTT connectionClosed
`try self.clientIdentityTrust = CertificateUtil.processCertFromP12File(path: path, certPassword: password) self.topicChannel = "user/(self.clientIdentityTrust.subject)/test1" self.mainTopicChannel = "user/(self.clientIdentityTrust.subject)/test2" let transport = MQTTCFSocketTransport() transport.certificates = self.clientIdentityTrust.certArray as? [Any] transport.host = Environment.Production.rawValue transport.port = UInt32(MQTTManager.kMQTTPort) self.mqtt.transport = transport
// var sslSettings: [String: NSObject] = [:] // sslSettings[kCFStreamSSLCertificates as String] = self.clientIdentityTrust.certArray // sslSettings[GCDAsyncSocketSSLProtocolVersionMin as String] = SSLProtocol.tlsProtocol12.rawValue as NSObject // self.mqtt.sslSettings = sslSettings self.mqtt.certificates = self.clientIdentityTrust.certArray as? [Any] self.mqtt.willTopic = self.topicChannel self.mqtt.willMsg = MQTTManager.kMessage .data(using: .utf8, allowLossyConversion: true) self.mqtt.userName = userId() self.mqtt.password = password
self.mqtt.connect()`
I don't think it is a certificate problem, because the connection is established.
The problem is the broker disconnect the client. You may want to check the broker logs. Which kind of broker do you use?
Make sure you have set willFlag
, willQos
, and willRetain
to appropriate values.
Thank you @ckrey . The tls flag needed to be set true.
It turned out the main problem was not to specify tls
//self.mqtt?.enableSSL = true
self.mqtt.tls = true;
I just switched from CocoaMQTT library to this library as framework also provides persistence. Though it seems like I have the setup ready (identical to the working version of CocoaMQTT), I still get an error.
Environment
Link to logs
https://gist.github.com/aratalcordtech/3167e6df0f5412ddd7958ac261189703
Expected behaviour
Connection should have been established.
Actual behaviour
Connection not established
Other information
I think it maybe because of the certificates which is why I am seeing NSPOSIXErrorDomain, but I am not sure. I was able to use the CocoaMQTT framework just fine. It was establishing the connection and working as expected.
I tried setting the certificates in both ways. Setting it into the transport.certificates and mqtt.certificates. Either gave a bad result.