Guest User

Untitled

a guest
Oct 6th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. class ConnectionFunctions{
  2. struct staticMQTT {
  3. static var mqttClient: CocoaMQTT!
  4. }
  5. func configureMQTT() {
  6. let clientID = "Tablet"
  7. let host = "192.168.2.106"
  8. let port = UInt16(1883)
  9. staticMQTT.mqttClient = CocoaMQTT(clientID: clientID, host: host, port: port)
  10. staticMQTT.mqttClient.username = ""
  11. staticMQTT.mqttClient.password = ""
  12. staticMQTT.mqttClient.keepAlive = 60
  13. staticMQTT.mqttClient.delegate = self
  14. }
  15. func sendMessage(topic:String, message:String){
  16. staticMQTT.mqttClient.publish(topic, withString: message)
  17. }
  18.  
  19. class MainViewController: UIViewController {
  20.  
  21. //MQTT Setup
  22. var mqttfuncs=ConnectionFunctions()
  23.  
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. mqttfuncs.configureMQTT()
  27. }
  28. @IBAction func ConnectingButton(_ sender: UIButton) {
  29. ConnectionFunctions.staticMQTT.mqttClient.connect()
  30. }
  31. @IBAction func Test(_ sender: UIButton) {
  32. mqttfuncs.sendMessage(topic: "Test", message: "main")
  33. mqttfuncs.receiveMessage(topic: "Test2")
  34. }
  35.  
  36. class ComponentDataController: UIViewController {
  37. let mqttfuncs = ConnectionFunctions()
  38. @IBAction func TestSending(_ sender: UIButton) {
  39. mqttfuncs.sendMessage(topic: "Test", message: "component")
  40. }
Add Comment
Please, Sign In to add comment