This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func connectToAWSIoT(clientId: String!) { | |
func mqttEventCallback(_ status: AWSIoTMQTTStatus ) { | |
switch status { | |
case .connecting: print("Connecting to AWS IoT") | |
case .connected: | |
print("Connected to AWS IoT") | |
// Register subscriptions here | |
// Publish a boot message if required | |
case .connectionError: print("AWS IoT connection error") | |
case .connectionRefused: print("AWS IoT connection refused") | |
case .protocolError: print("AWS IoT protocol error") | |
case .disconnected: print("AWS IoT disconnected") | |
case .unknown: print("AWS IoT unknown state") | |
default: print("Error - unknown MQTT state") | |
} | |
} | |
// Ensure connection gets performed background thread (so as not to block the UI) | |
DispatchQueue.global(qos: .background).async { | |
do { | |
print("Attempting to connect to IoT device gateway with ID = \(clientId)") | |
let dataManager = AWSIoTDataManager(forKey: "kDataManager") | |
dataManager.connectUsingWebSocket(withClientId: clientId, | |
cleanSession: true, | |
statusCallback: mqttEventCallback) | |
} catch { | |
print("Error, failed to connect to device gateway => \(error!)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment