Skip to content

Commit

Permalink
code in the mqtt user and password
Browse files Browse the repository at this point in the history
  • Loading branch information
goatchurchprime committed Jul 16, 2024
1 parent d9ff4ce commit 7d3fde4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
20 changes: 14 additions & 6 deletions addons/mqtt/mqtt.gd
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ func set_last_will(stopic, smsg, retain=false, qos=0):
if verbose_level:
print("LASTWILL%s topic=%s msg=%s" % [ " <retain>" if retain else "", stopic, smsg])

func set_user_pass(suser, spswd):
if suser != null:
self.user = suser.to_ascii_buffer()
self.pswd = spswd.to_ascii_buffer()
else:
self.user = null
self.pswd = null

func firstmessagetoserver():
var clean_session = true
var msg = PackedByteArray()
Expand Down Expand Up @@ -227,12 +235,12 @@ func firstmessagetoserver():
msg.append(len(self.lw_msg) & 0xFF)
msg.append_array(self.lw_msg)
if self.user != null:
msg.append(self.user.length() >> 8)
msg.append(self.user.length() & 0xFF)
msg.append_array(self.user.to_ascii_buffer())
msg.append(self.pswd.length() >> 8)
msg.append(self.pswd.length() & 0xFF)
msg.append_array(self.pswd.to_ascii_buffer())
msg.append(len(self.user) >> 8)
msg.append(len(self.user) & 0xFF)
msg.append_array(self.user)
msg.append(len(self.pswd) >> 8)
msg.append(len(self.pswd) & 0xFF)
msg.append_array(self.pswd)
return msg

func cleanupsockets(retval=false):
Expand Down
11 changes: 9 additions & 2 deletions mqttexample.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ func _on_button_connect_toggled(button_pressed):
$VBox/HBoxLastwill/lastwillretain.button_pressed)
else:
$MQTT.set_last_will("", "", false)


if $VBox/HBoxBroker/brokeruser.text:
$MQTT.set_user_pass($VBox/HBoxBroker/brokeruser.text,
$VBox/HBoxBroker/brokerpswd.text)
else:
$MQTT.set_user_pass(null, null)

$VBox/HBoxBrokerControl/status.text = "connecting..."
var brokerurl = $VBox/HBoxBroker/brokeraddress.text
var protocol = $VBox/HBoxBroker/brokerprotocol.get_item_text($VBox/HBoxBroker/brokerprotocol.selected)
$MQTT.connect_to_broker("%s%s:%s" % [protocol, brokerurl, $VBox/HBoxBroker/brokerport.text])

else:
$VBox/HBoxBrokerControl/status.text = "disconnecting..."
#$VBox/HBoxBrokerControl/status.text = "disconnecting..."
$MQTT.disconnect_from_server()


func brokersettingsactive(active):
$VBox/HBoxBroker/brokeraddress.editable = active
Expand Down
14 changes: 14 additions & 0 deletions mqttexample.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ layout_mode = 2
size_flags_horizontal = 3
text = "broker.hivemq.com"

[node name="Label4" type="Label" parent="VBox/HBoxBroker"]
layout_mode = 2
text = "User:"

[node name="brokeruser" type="LineEdit" parent="VBox/HBoxBroker"]
layout_mode = 2

[node name="Label5" type="Label" parent="VBox/HBoxBroker"]
layout_mode = 2
text = "Pswd:"

[node name="brokerpswd" type="LineEdit" parent="VBox/HBoxBroker"]
layout_mode = 2

[node name="Label2" type="Label" parent="VBox/HBoxBroker"]
layout_mode = 2
text = "Port: "
Expand Down

0 comments on commit 7d3fde4

Please sign in to comment.