From bbc34940d36496d1c921c854b234864d904314e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sun, 1 Dec 2024 09:19:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E4=BA=86=E6=8F=90=E5=8D=87=E6=80=A7?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E5=9C=A8=E5=90=8C=E6=AD=A5=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E6=97=B6=E8=A7=84=E9=81=BF=E5=8D=A1UI?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=EF=BC=8C=E6=89=80=E6=9C=89await?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=9C=BA=E9=83=BD=E8=AE=BE=E7=BD=AEConfigure?= =?UTF-8?q?Await(false)=EF=BC=8C=E5=BC=80=E5=90=AFCA2007=E5=B9=B6=E8=A7=86?= =?UTF-8?q?=E4=B8=BA=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF=E3=80=82=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84await=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.MQTT/AliyunMqttClient.cs | 10 +++---- NewLife.MQTT/Clusters/ClusterNode.cs | 30 ++++++++++---------- NewLife.MQTT/Handlers/IMqttHandler.cs | 8 +++--- NewLife.MQTT/MqttClient.cs | 22 +++++++------- NewLife.MQTT/NewLife.MQTT.csproj | 6 ++-- NewLife.MqttServer/NewLife.MqttServer.csproj | 2 +- README.md | 8 +++--- Test/Test.csproj | 2 +- XUnitTestClient/XUnitTestClient.csproj | 2 +- 9 files changed, 46 insertions(+), 44 deletions(-) diff --git a/NewLife.MQTT/AliyunMqttClient.cs b/NewLife.MQTT/AliyunMqttClient.cs index 141cfb5..cbe2790 100644 --- a/NewLife.MQTT/AliyunMqttClient.cs +++ b/NewLife.MQTT/AliyunMqttClient.cs @@ -47,15 +47,15 @@ public AliyunMqttClient(String productKey, String deviceName, String deviceSecre /// public virtual async Task PostProperty(Object data) { - await SubscribeAsync($"/sys/{ProductKey}/{DeviceName}/thing/event/property/post_reply", OnPostProperty); + await SubscribeAsync($"/sys/{ProductKey}/{DeviceName}/thing/event/property/post_reply", OnPostProperty).ConfigureAwait(false); await PublishAsync($"/sys/{ProductKey}/{DeviceName}/thing/event/property/post", new { //id = 1, @params = data, method = "thing.event.property.post" - }); + }).ConfigureAwait(false); - await SubscribeAsync($"/sys/{ProductKey}/{DeviceName}/thing/service/property/set", OnSetProperty); + await SubscribeAsync($"/sys/{ProductKey}/{DeviceName}/thing/service/property/set", OnSetProperty).ConfigureAwait(false); } /// 收到属性上报 @@ -72,8 +72,8 @@ public virtual async Task PostProperty(Object data) /// public async Task SyncTime() { - await SubscribeAsync($"/ext/ntp/{ProductKey}/{DeviceName}/response", OnSyncTime); - await PublishAsync($"/ext/ntp/{ProductKey}/{DeviceName}/request", new { version = "1.0" }); + await SubscribeAsync($"/ext/ntp/{ProductKey}/{DeviceName}/response", OnSyncTime).ConfigureAwait(false); + await PublishAsync($"/ext/ntp/{ProductKey}/{DeviceName}/request", new { version = "1.0" }).ConfigureAwait(false); } /// 收到时间同步 diff --git a/NewLife.MQTT/Clusters/ClusterNode.cs b/NewLife.MQTT/Clusters/ClusterNode.cs index 8414fee..d89807d 100644 --- a/NewLife.MQTT/Clusters/ClusterNode.cs +++ b/NewLife.MQTT/Clusters/ClusterNode.cs @@ -77,11 +77,11 @@ private void Init() /// 回响 /// /// - public async Task Echo(String msg) + public Task Echo(String msg) { Init(); - return await Client.InvokeAsync("Cluster/Echo", msg).ConfigureAwait(false); + return Client.InvokeAsync("Cluster/Echo", msg); } /// 加入集群 @@ -95,61 +95,61 @@ private void Init() } /// 心跳 - public async Task Ping(NodeInfo info) + public Task Ping(NodeInfo info) { Init(); _myNode = info; if (_remoteNode == null) - return await Join(info); + return Join(info); else - return await Client.InvokeAsync("Cluster/Ping", info).ConfigureAwait(false); + return Client.InvokeAsync("Cluster/Ping", info); } /// 离开集群 - public async Task Leave(NodeInfo info) + public Task Leave(NodeInfo info) { Init(); - return await Client.InvokeAsync("Cluster/Leave", info).ConfigureAwait(false); + return Client.InvokeAsync("Cluster/Leave", info); } /// 订阅 /// /// - public async Task Subscribe(SubscriptionInfo[] infos) + public Task Subscribe(SubscriptionInfo[] infos) { Init(); - return await Client.InvokeAsync("Cluster/Subscribe", infos).ConfigureAwait(false); + return Client.InvokeAsync("Cluster/Subscribe", infos); } /// 退订 /// /// - public async Task Unsubscribe(SubscriptionInfo[] infos) + public Task Unsubscribe(SubscriptionInfo[] infos) { Init(); - return await Client.InvokeAsync("Cluster/Unsubscribe", infos).ConfigureAwait(false); + return Client.InvokeAsync("Cluster/Unsubscribe", infos); } /// 心跳 /// - public async Task Ping() + public Task Ping() { Init(); - return await Client.InvokeAsync("Cluster/Ping").ConfigureAwait(false); + return Client.InvokeAsync("Cluster/Ping"); } /// 发布 - public async Task Publish(PublishInfo info) + public Task Publish(PublishInfo info) { Init(); - return await Client.InvokeAsync("Cluster/Publish", info).ConfigureAwait(false); + return Client.InvokeAsync("Cluster/Publish", info); } #endregion } diff --git a/NewLife.MQTT/Handlers/IMqttHandler.cs b/NewLife.MQTT/Handlers/IMqttHandler.cs index 148c14d..2307720 100644 --- a/NewLife.MQTT/Handlers/IMqttHandler.cs +++ b/NewLife.MQTT/Handlers/IMqttHandler.cs @@ -189,7 +189,7 @@ protected virtual UnsubAck OnUnsubscribe(UnsubscribeMessage message) /// 消息数据 /// 服务质量 /// - public async Task PublishAsync(String topic, Object data, QualityOfService qos = QualityOfService.AtMostOnce) + public Task PublishAsync(String topic, Object data, QualityOfService qos = QualityOfService.AtMostOnce) { var pk = data as IPacket; if (pk == null && data != null) pk = Encoder.Encode(data); @@ -202,7 +202,7 @@ protected virtual UnsubAck OnUnsubscribe(UnsubscribeMessage message) QoS = qos, }; - return await PublishAsync(message); + return PublishAsync(message); } /// 发布消息 @@ -211,7 +211,7 @@ protected virtual UnsubAck OnUnsubscribe(UnsubscribeMessage message) /// 服务质量 /// 允许消息交换 /// - public async Task PublishAsync(String topic, Object data, Boolean allowExchange, QualityOfService qos = QualityOfService.AtMostOnce) + public Task PublishAsync(String topic, Object data, Boolean allowExchange, QualityOfService qos = QualityOfService.AtMostOnce) { var pk = data as IPacket; if (pk == null && data != null) pk = Encoder.Encode(data); @@ -231,7 +231,7 @@ protected virtual UnsubAck OnUnsubscribe(UnsubscribeMessage message) ClusterExchange?.Publish(Session, message); } - return await PublishAsync(message); + return PublishAsync(message); } /// 发布消息 diff --git a/NewLife.MQTT/MqttClient.cs b/NewLife.MQTT/MqttClient.cs index f608d51..c1f6124 100644 --- a/NewLife.MQTT/MqttClient.cs +++ b/NewLife.MQTT/MqttClient.cs @@ -358,7 +358,7 @@ private void Client_Received(Object sender, ReceivedEventArgs e) /// 连接服务端 /// - public async Task ConnectAsync() + public Task ConnectAsync() { if (ClientId.IsNullOrEmpty()) throw new ArgumentNullException(nameof(ClientId)); @@ -370,7 +370,7 @@ public async Task ConnectAsync() CleanSession = CleanSession, }; - return await ConnectAsync(message); + return ConnectAsync(message); } /// 连接服务端 @@ -458,7 +458,7 @@ private void Client_Closed(Object sender, EventArgs e) /// 消息数据 /// 服务质量 /// - public async Task PublishAsync(String topic, Object data, QualityOfService qos = QualityOfService.AtMostOnce) + public Task PublishAsync(String topic, Object data, QualityOfService qos = QualityOfService.AtMostOnce) { var pk = data as IPacket; if (pk == null && data != null) pk = Encoder.Encode(data); @@ -471,7 +471,7 @@ private void Client_Closed(Object sender, EventArgs e) QoS = qos, }; - return await PublishAsync(message); + return PublishAsync(message); } /// 发布消息 @@ -499,22 +499,22 @@ private void Client_Closed(Object sender, EventArgs e) /// 主题过滤器 /// 收到该主题消息时的回调 /// - public async Task SubscribeAsync(String topicFilter, Action? callback = null) + public Task SubscribeAsync(String topicFilter, Action? callback = null) { var subscription = new Subscription(topicFilter, QualityOfService.AtMostOnce); - return await SubscribeAsync([subscription], callback); + return SubscribeAsync([subscription], callback); } /// 订阅主题 /// 主题过滤器 /// 服务质量 /// - public async Task SubscribeAsync(String[] topicFilters, QualityOfService qos = QualityOfService.AtMostOnce) + public Task SubscribeAsync(String[] topicFilters, QualityOfService qos = QualityOfService.AtMostOnce) { var subscriptions = topicFilters.Select(e => new Subscription(e, qos)).ToList(); - return await SubscribeAsync(subscriptions); + return SubscribeAsync(subscriptions); } /// 订阅主题 @@ -532,7 +532,7 @@ private void Client_Closed(Object sender, EventArgs e) Requests = subscriptions, }; - var rs = (await SendAsync(message)) as SubAck; + var rs = (await SendAsync(message).ConfigureAwait(false)) as SubAck; if (rs != null) { foreach (var item in subscriptions) @@ -555,7 +555,7 @@ private void Client_Closed(Object sender, EventArgs e) TopicFilters = topicFilters, }; - var rs = (await SendAsync(message)) as UnsubAck; + var rs = (await SendAsync(message).ConfigureAwait(false)) as UnsubAck; if (rs != null) { @@ -581,7 +581,7 @@ private void Client_Closed(Object sender, EventArgs e) var message = new PingRequest(); - var rs = (await SendAsync(message)) as PingResponse; + var rs = (await SendAsync(message).ConfigureAwait(false)) as PingResponse; return rs; } diff --git a/NewLife.MQTT/NewLife.MQTT.csproj b/NewLife.MQTT/NewLife.MQTT.csproj index bfbf023..eb64b9c 100644 --- a/NewLife.MQTT/NewLife.MQTT.csproj +++ b/NewLife.MQTT/NewLife.MQTT.csproj @@ -19,6 +19,8 @@ enable True ..\Doc\newlife.snk + latest + CA2007 @@ -65,8 +67,8 @@ - - + + diff --git a/NewLife.MqttServer/NewLife.MqttServer.csproj b/NewLife.MqttServer/NewLife.MqttServer.csproj index 817eb98..1d594a4 100644 --- a/NewLife.MqttServer/NewLife.MqttServer.csproj +++ b/NewLife.MqttServer/NewLife.MqttServer.csproj @@ -21,7 +21,7 @@ - + diff --git a/README.md b/README.md index c91aeb5..9f3b268 100644 --- a/README.md +++ b/README.md @@ -76,13 +76,13 @@ var client = new MqttClient ClientId = Guid.NewGuid() + "", }; -await client.ConnectAsync(); +await client.ConnectAsync().ConfigureAwait(false); // 订阅“/test”主题 var rt = await client.SubscribeAsync("/test", (e) => { XTrace.WriteLine("收到消息:" + "/test/# =>" + e.Topic + ":" + e.Payload.ToStr()); -}); +}).ConfigureAwait(false); // 每2秒向“/test”主题发布一条消息 while (true) @@ -90,13 +90,13 @@ while (true) try { var msg = "学无先后达者为师" + Rand.NextString(8); - await client.PublishAsync("/test", msg); + await client.PublishAsync("/test", msg).ConfigureAwait(false); } catch (Exception ex) { XTrace.WriteException(ex); } - await Task.Delay(2000); + await Task.Delay(2000).ConfigureAwait(false); } ``` 客户端连接服务端有几个要素:`服务端地址`、`用户名`、`密码`、`客户端标识`,然后通过`ConnectAsync`连接服务端。 diff --git a/Test/Test.csproj b/Test/Test.csproj index d64eab9..1c3143c 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -16,7 +16,7 @@ latest - + diff --git a/XUnitTestClient/XUnitTestClient.csproj b/XUnitTestClient/XUnitTestClient.csproj index e8f0eb3..01dd5fd 100644 --- a/XUnitTestClient/XUnitTestClient.csproj +++ b/XUnitTestClient/XUnitTestClient.csproj @@ -10,7 +10,7 @@ - +