Platforms to show: All Mac Windows Linux Cross-Platform

/CURL/MQTT


Required plugins for this example: MBS CURL Plugin

Last modified Sun, 26th Jul 2025.

You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /CURL/MQTT

Download this example: MQTT.zip

Project "MQTT.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits DesktopWindow
Control URLField Inherits DesktopTextField
ControlInstance URLField Inherits DesktopTextField
EventHandler Sub TextChanged() ConnectButton.Enabled = URLField.Text.Length > 0 End EventHandler
End Control
Control ConnectButton Inherits DesktopButton
ControlInstance ConnectButton Inherits DesktopButton
EventHandler Sub Pressed() Var c As New CURLReceiveMQTT c.OptionURL = URLField.Text c.OptionConnectionTimeout = 5 c.OptionTCPKeepAlive = True // ping server every 30 seconds to keep connection alive c.OptionUpkeepIntervalMS = 30000 curl = c // this runs asynchron in background and calls Write event when needed Call CURLSMultiMBS.SharedInstance.AddCURL(C) End EventHandler
End Control
Control PayloadField Inherits DesktopTextField
ControlInstance PayloadField Inherits DesktopTextField
EventHandler Sub TextChanged() SendButton.Enabled = Me.Text.Length > 0 End EventHandler
End Control
Control SendButton Inherits DesktopButton
ControlInstance SendButton Inherits DesktopButton
EventHandler Sub Pressed() Me.Enabled = False var c As New CURLSMBS c.OptionURL = URLField.Text c.OptionPostFields = PayloadField.Text var e As Integer = c.Perform var debugLog As String = c.DebugMessages.ReplaceLineEndings(EndOfLine) var Output As String = c.OutputData If e = 0 Then List.AddRow "Sent." PayloadField.Text = "" Else var LasterrorMessage As String = c.LasterrorMessage List.AddRow "Sent failed: "+LasterrorMessage Break MessageBox "Failed to send with error " + e.ToString + EndOfLine+EndOfLine + LasterrorMessage End If End EventHandler
End Control
Control List Inherits DesktopListBox
ControlInstance List Inherits DesktopListBox
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action() Dim d As DateTime = DateTime.Now List.AddRow str(d.SecondsFrom1970-start.SecondsFrom1970, "0.0")+" seconds" End EventHandler
End Control
EventHandler Sub Opening() Start = DateTime.now End EventHandler
Property Start As datetime
Property curl As CURLReceiveMQTT
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
Class CURLReceiveMQTT Inherits CURLSMBS
EventHandler Sub Finished(Result as Integer) #Pragma BackgroundTasks False System.DebugLog CurrentMethodName +": "+result.ToString Var LastErrorMessage As String = Me.LasterrorMessage var debugLog As String = Me.DebugMessages.ReplaceLineEndings(EndOfLine) Var Output As String = Me.OutputData MainWindow.List.AddRow "Finished: "+result.ToString End EventHandler
EventHandler Function Write(data as string, dataSize as Integer) As integer #Pragma BackgroundTasks False // we received data! System.DebugLog CurrentMethodName // first two bytes are length, then channel and content Var m As MemoryBlock = data m.LittleEndian = False Var l As Integer = m.Int16Value(0) Var c As String = MidB(data, 3, l) Var d As String = MidB(data, 3 + l) MainWindow.List.AddRow "Received: "+c+" "+d Return dataSize End EventHandler
End Class
End Project

Download this example: MQTT.zip

The items on this page are in the following plugins: MBS CURL Plugin.


The biggest plugin in space...