Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import UIKit
  2. import CocoaMQTT
  3.  
  4. class ViewController: UIViewController, CocoaMQTTDelegate {
  5.  
  6. let defaultHost = "35.*******"
  7.  
  8. var mqtt: CocoaMQTT!
  9.  
  10. override func viewDidLoad() {
  11. super.viewDidLoad()
  12. // Do any additional setup after loading the view, typically from a nib.
  13.  
  14. mqttSetting()
  15.  
  16.  
  17. }
  18.  
  19.  
  20. func mqttSetting() {
  21. let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
  22. mqtt = CocoaMQTT(clientID: clientID, host: defaultHost, port: 1883)
  23. mqtt!.username = "*****"
  24. mqtt!.password = "*****"
  25. mqtt!.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
  26. mqtt!.keepAlive = 60
  27. mqtt!.delegate = self
  28. }
  29.  
  30.  
  31.  
  32. func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
  33.  
  34. if ack == .accept {
  35. mqtt.subscribe("*************")
  36.  
  37.  
  38. }
  39. }
  40.  
  41. func mqtt(_ mqtt: CocoaMQTT, didStateChangeTo state: CocoaMQTTConnState) {
  42.  
  43. }
  44.  
  45.  
  46. func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16 ) {
  47. print(message)
  48. }
  49.  
  50. func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopic topic: String) {
  51. }
  52.  
  53. func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
  54. print("(err.debugDescription)")
  55. }
  56.  
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement