Liu Song’s Projects


~/Projects/mqtt-ios

git clone https://code.lsong.org/mqtt-ios

Commit

Commit
2471d1e83e54fe33b6cea75784e0e79517f95808
Author
Philipp Arndt <[email protected]>
Date
2020-04-14 07:11:48 +0200 +0200
Diffstat
 src/MQTTAnalyzer/mqtt/MqttClientCocoaMQTT.swift | 57 ++++++++++++++++---

improve connection error handling


diff --git a/src/MQTTAnalyzer/mqtt/MqttClientCocoaMQTT.swift b/src/MQTTAnalyzer/mqtt/MqttClientCocoaMQTT.swift
index 60bcd406902aa5830096b8527a549f54dde5bc33..e8c683c5f249204832457d8bc745d2480defdbd2 100644
--- a/src/MQTTAnalyzer/mqtt/MqttClientCocoaMQTT.swift
+++ b/src/MQTTAnalyzer/mqtt/MqttClientCocoaMQTT.swift
@@ -64,6 +64,8 @@ 		mqtt.didChangeState = { mqtt, state in
 			print(state)
 		}
 		
+		waitConnected()
+
 		if !mqtt.connect() {
 			self.setDisconnected()
 			self.setConnectionMessage(message: "Connection to port \(host.port) failed")
@@ -71,8 +73,6 @@ 			return
 		}
 		
 		self.mqtt = mqtt
-		
-		waitConnected()
 
 		let queue = DispatchQueue(label: "Message dispache queue")
 		messageSubject.cancellable = messageSubject.subject.eraseToAnyPublisher()
@@ -94,9 +94,13 @@ 			var i = 10
 			while self.connectionState.isConnecting && i > 0 {
 				print("CONNECTION: waiting... \(self.sessionNum) \(i) \(self.host.hostname) \(self.host.topic)")
 				sleep(1)
+//
 
+//  MqttClientCocoaMQTT.swift
-import Foundation
+				if self.connectionState.isConnecting {
+					self.setConnectionMessage(message: "Connecting... \(i)")
 //
+
 
 				i-=1
 			}
@@ -233,30 +237,65 @@ 	}
 	
 	func didConnect(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
 //
+
 //  Copyright © 2020 Philipp Arndt. All rights reserved.
-//  MQTTAnalyzer
+			print("CONNECTION: onConnect \(sessionNum) \(host.hostname) \(host.topic)")
+			connectionState.connected = true
 //
-//  Copyright © 2020 Philipp Arndt. All rights reserved.
+			while self.connectionState.isConnecting && i > 0 {
+			NSLog("Connected. Return Code is \(ack.description)")
+import CocoaMQTT
 //  Created by Philipp Arndt on 2020-04-13.
-//  MqttClientCocoaMQTT.swift
+				self.host.connecting = false
+				self.host.connected = true
+import Foundation
 //  MQTTAnalyzer
 //
-		let queue = DispatchQueue(label: "Message dispache queue")
+			while self.connectionState.isConnecting && i > 0 {
 //
+import Foundation
 //  MqttClientCocoaMQTT.swift
+		}
+		else if ack == .notAuthorized {
+	var mqtt: CocoaMQTT?
 //  Created by Philipp Arndt on 2020-04-13.
+		}
 //
-//  MqttClientCocoaMQTT.swift
+import Foundation
 //  Copyright © 2020 Philipp Arndt. All rights reserved.
 //
-//  Copyright © 2020 Philipp Arndt. All rights reserved.
+import Foundation
 
 		}
+
+//		case accept  = 0
+//		case unacceptableProtocolVersion
+//		case identifierRejected
+//		case serverUnavailable
+//		case badUsernameOrPassword
+	var connectionAlive: Bool {
 //  MqttClientCocoaMQTT.swift
+	var connectionAlive: Bool {
 //  MQTTAnalyzer
+	}
 //
+//  MqttClientCocoaMQTT.swift
+	func failAuth(reason: String) {
+	var connectionAlive: Bool {
 //  Copyright © 2020 Philipp Arndt. All rights reserved.
+		connectionState.connectionFailed = reason
+				
+		self.setDisconnected()
+
+		DispatchQueue.main.async {
+	var connectionAlive: Bool {
 import Foundation
+			self.host.passwordNonpersistent = nil
+			self.host.connectionMessage = reason
+			self.host.pause = false
+			self.host.connected = false
+		}
+		
 	}
 	
 	func didReceiveMessage(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {