如何在 Perl 模块 Net::MQTT::Simple(MQTT 接口)中设置 ClientID?
How to set a ClientID in Perl module Net::MQTT::Simple (MQTT interface)?
我想使用 Perl 模块 Net::MQTT::Simple
将 MQTT 消息发送到 MQTT 服务器。这是一个基于 CPAN documentation of Net::MQTT::Simple:
的简单 MVP 脚本
#!/usr/bin perl
use warnings;
use strict;
use autodie;
use Net::MQTT::Simple;
# Allow unencrypted connection with credentials
$ENV{MQTT_SIMPLE_ALLOW_INSECURE_LOGIN} = 1;
# Connect to broker
my $mqtt = Net::MQTT::Simple->new('localhost:1883');
my $mqtt_username = 'username';
my $mqtt_password = 'verysecretpassword';
# Depending if authentication is required, login to the broker
if($mqtt_username and $mqtt_password) {
$mqtt->login($mqtt_username, $mqtt_password);
}
# Publish a message
$mqtt->publish("home/temperature", "20.5");
$mqtt->disconnect();
我的问题是:我需要在传输中指定客户端 ID,以便接收 MQTT 服务器正确处理消息。任何帮助表示赞赏!
丹尼尔
编辑:好的,已回答。不可能。我想我必须坚持使用我当前的解决方案,从 Per 脚本中执行 mosquitto_pub
,让我指定一个客户端 ID。
查看来源 code 你不知道。
该代码生成一个随机客户端 ID,无法更新它。
我建议您考虑一个替代方案,例如Python Paho 图书馆。
为 _client_identifier()
提供覆盖可能会解决您的问题:
#!/usr/bin perl
use warnings;
use strict;
use autodie;
use Net::MQTT::Simple;
package Net::MQTT::Simple::ID;
our @ISA = 'Net::MQTT::Simple';
sub _client_identifier{
return 'My_custom_client_id';
}
package main;
# Allow unencrypted connection with credentials
$ENV{MQTT_SIMPLE_ALLOW_INSECURE_LOGIN} = 1;
# Connect to broker
my $mqtt = Net::MQTT::Simple::ID->new('localhost:1883');
my $mqtt_username = 'username';
my $mqtt_password = 'verysecretpassword';
# Depending if authentication is required, login to the broker
if($mqtt_username and $mqtt_password) {
$mqtt->login($mqtt_username, $mqtt_password);
}
# Publish a message
$mqtt->publish("home/temperature", "20.5");
$mqtt->disconnect();
__END__
nc -lv 127.0.0.1 1883 | od -c
Listening on localhost 1883
Connection received on localhost 55172
0000000 020 = [=10=] 004 M Q T T 004 302 [=10=] < [=10=] 023 M y
0000020 _ c u s t o m _ c l i e n t _ i
0000040 d [=10=] \b u s e r n a m e [=10=] 022 v e r
0000060 y s e c r e t p a s s w o r d 0
0000100 026 [=10=] 020 h o m e / t e m p e r a t
0000120 u r e 2 0 . 5 340 [=10=]
0000131
您需要覆盖 Net::MQTT::Simple 的 _client_identifier 方法。
一行即可完成:
*{Net::MQTT::Simple::_client_identifier} = sub { return 'my_client_id'; };
我想使用 Perl 模块 Net::MQTT::Simple
将 MQTT 消息发送到 MQTT 服务器。这是一个基于 CPAN documentation of Net::MQTT::Simple:
#!/usr/bin perl
use warnings;
use strict;
use autodie;
use Net::MQTT::Simple;
# Allow unencrypted connection with credentials
$ENV{MQTT_SIMPLE_ALLOW_INSECURE_LOGIN} = 1;
# Connect to broker
my $mqtt = Net::MQTT::Simple->new('localhost:1883');
my $mqtt_username = 'username';
my $mqtt_password = 'verysecretpassword';
# Depending if authentication is required, login to the broker
if($mqtt_username and $mqtt_password) {
$mqtt->login($mqtt_username, $mqtt_password);
}
# Publish a message
$mqtt->publish("home/temperature", "20.5");
$mqtt->disconnect();
我的问题是:我需要在传输中指定客户端 ID,以便接收 MQTT 服务器正确处理消息。任何帮助表示赞赏! 丹尼尔
编辑:好的,已回答。不可能。我想我必须坚持使用我当前的解决方案,从 Per 脚本中执行 mosquitto_pub
,让我指定一个客户端 ID。
查看来源 code 你不知道。
该代码生成一个随机客户端 ID,无法更新它。
我建议您考虑一个替代方案,例如Python Paho 图书馆。
为 _client_identifier()
提供覆盖可能会解决您的问题:
#!/usr/bin perl
use warnings;
use strict;
use autodie;
use Net::MQTT::Simple;
package Net::MQTT::Simple::ID;
our @ISA = 'Net::MQTT::Simple';
sub _client_identifier{
return 'My_custom_client_id';
}
package main;
# Allow unencrypted connection with credentials
$ENV{MQTT_SIMPLE_ALLOW_INSECURE_LOGIN} = 1;
# Connect to broker
my $mqtt = Net::MQTT::Simple::ID->new('localhost:1883');
my $mqtt_username = 'username';
my $mqtt_password = 'verysecretpassword';
# Depending if authentication is required, login to the broker
if($mqtt_username and $mqtt_password) {
$mqtt->login($mqtt_username, $mqtt_password);
}
# Publish a message
$mqtt->publish("home/temperature", "20.5");
$mqtt->disconnect();
__END__
nc -lv 127.0.0.1 1883 | od -c
Listening on localhost 1883
Connection received on localhost 55172
0000000 020 = [=10=] 004 M Q T T 004 302 [=10=] < [=10=] 023 M y
0000020 _ c u s t o m _ c l i e n t _ i
0000040 d [=10=] \b u s e r n a m e [=10=] 022 v e r
0000060 y s e c r e t p a s s w o r d 0
0000100 026 [=10=] 020 h o m e / t e m p e r a t
0000120 u r e 2 0 . 5 340 [=10=]
0000131
您需要覆盖 Net::MQTT::Simple 的 _client_identifier 方法。
一行即可完成:
*{Net::MQTT::Simple::_client_identifier} = sub { return 'my_client_id'; };