首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MqttNet基本示例

MqttNet基本示例
EN

Stack Overflow用户
提问于 2022-06-21 08:24:07
回答 2查看 3.2K关注 0票数 4

我必须创建一个非常基本的Mqtt演示,因此我遵循并下载了示例。

它工作得很完美,但它是一个带有Net5.0的控制台应用程序。

我必须让它在wpf 4.5.2上工作。解决方案。根据这个,这是可能的。

因此,以上面的解决方案为例

我创建了一个具有相同参考性的项目。

对于每个项目,我还添加了正确的使用语句,如示例中所示。

所以一切都应该是正确的,但是当我粘贴服务器的代码时,我会得到这些错误

代码语言:javascript
复制
MqttServerOptionsBuilder options = new MqttServerOptionsBuilder()
           .WithDefaultEndpoint()
           .WithDefaultEndpointPort(707)
           .WithConnectionValidator(OnNewConnection)
           .WithApplicationMessageInterceptor(OnNewMessage);


        IMqttServer mqttServer = new MqttFactory().CreateMqttServer();

此外,对于客户端,我还会得到其他错误。

问题出在哪里?谢谢

-加

按照要求,这里是错误

代码语言:javascript
复制
(1) Error   CS1061  'IManagedMqttClient' does not contain a definition for 'ConnectedHandler' and no accessible extension method 'ConnectedHandler' accepting a first argument of type 'IManagedMqttClient' could be found (are you missing a using directive or an assembly reference?)    Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  43  25  IntelliSense    Active

(2) Error   CS0246  The type or namespace name 'MqttClientConnectedHandlerDelegate' could not be found (are you missing a using directive or an assembly reference?)    Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  43  48  IntelliSense    Active

(3) Error   CS0103  The name 'OnConnected' does not exist in the current context    Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  43  83  IntelliSense    Active

(4) Error   CS1061  'IManagedMqttClient' does not contain a definition for 'DisconnectedHandler' and no accessible extension method 'DisconnectedHandler' accepting a first argument of type 'IManagedMqttClient' could be found (are you missing a using directive or an assembly reference?)  Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  44  25  IntelliSense    Active

(5) Error   CS0246  The type or namespace name 'MqttClientDisconnectedHandlerDelegate' could not be found (are you missing a using directive or an assembly reference?) Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  44  51  IntelliSense    Active

(6) Error   CS0103  The name 'OnDisconnected' does not exist in the current context Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  44  89  IntelliSense    Active

(7) Error   CS1061  'IManagedMqttClient' does not contain a definition for 'ConnectingFailedHandler' and no accessible extension method 'ConnectingFailedHandler' accepting a first argument of type 'IManagedMqttClient' could be found (are you missing a using directive or an assembly reference?)  Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  45  25  IntelliSense    Active

(8) Error   CS0246  The type or namespace name 'ConnectingFailedHandlerDelegate' could not be found (are you missing a using directive or an assembly reference?)   Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  45  55  IntelliSense    Active

(9) Error   CS0103  The name 'OnConnectingFailed' does not exist in the current context Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  45  87  IntelliSense    Active

(10) Error  CS1061  'IManagedMqttClient' does not contain a definition for 'ApplicationMessageReceivedHandler' and no accessible extension method 'ApplicationMessageReceivedHandler' accepting a first argument of type 'IManagedMqttClient' could be found (are you missing a using directive or an assembly reference?)  Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  47  25  IntelliSense    Active

(11) Error  CS0246  The type or namespace name 'MqttApplicationMessageReceivedHandlerDelegate' could not be found (are you missing a using directive or an assembly reference?) Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  47  65  IntelliSense    Active

(12) Error  CS1061  'IManagedMqttClient' does not contain a definition for 'PublishAsync' and no accessible extension method 'PublishAsync' accepting a first argument of type 'IManagedMqttClient' could be found (are you missing a using directive or an assembly reference?)    Client  C:\Development\MqttDemo\MqttDemo\Client\MainWindow.xaml.cs  56  29  IntelliSense    Active
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-28 13:00:08

MQTT正在迅速成为物联网部署的主要协议之一。

MQTT有两个不同的变体和几个版本。

MQTT v3.1.0 -

MQTT v3.1.1 -常用

MQTT v5 -目前限制使用

MQTT-SN -见后面的说明

下面的示例使用版本3.0.16。MQTT v3.1.1是常用的版本。v3.10和3.1.1之间的差别很小。所以降低你的MQTT级别来解决这个问题。

代码语言:javascript
复制
Install-Package MQTTnet -Version 3.0.16

客户端:

代码语言:javascript
复制
Install-Package MQTTnet.Extensions.ManagedClient -Version 3.0.16

参考文献:link1link2link3link4

票数 2
EN

Stack Overflow用户

发布于 2022-09-12 11:17:08

您试图在旧版本3.1.x示例中使用MQTTNet新版本4.0.1.184。

如果您想使用最新的MQTTNet (4.x)版本,那么使用最新的示例。就像他们的github页面上的那个- Samples.cs

MQTTNet (客户端)版本与MQTT (协议)版本无关,而且据了解它们是可互操作的。例如,MQTT 3.1.1可以与MQTTNet 4.x客户端一起使用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72697486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档