Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import CocoaMQTT
- class ViewController: UIViewController, CocoaMQTTDelegate {
- let defaultHost = "35.*******"
- var mqtt: CocoaMQTT!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- mqttSetting()
- }
- func mqttSetting() {
- let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
- mqtt = CocoaMQTT(clientID: clientID, host: defaultHost, port: 1883)
- mqtt!.username = "*****"
- mqtt!.password = "*****"
- mqtt!.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
- mqtt!.keepAlive = 60
- mqtt!.delegate = self
- }
- func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
- if ack == .accept {
- mqtt.subscribe("*************")
- }
- }
- func mqtt(_ mqtt: CocoaMQTT, didStateChangeTo state: CocoaMQTTConnState) {
- }
- func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16 ) {
- print(message)
- }
- func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopic topic: String) {
- }
- func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
- print("(err.debugDescription)")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement