diff --git a/NewLife.MQTT/Handlers/IMqttHandler.cs b/NewLife.MQTT/Handlers/IMqttHandler.cs index 2307720..d458f93 100644 --- a/NewLife.MQTT/Handlers/IMqttHandler.cs +++ b/NewLife.MQTT/Handlers/IMqttHandler.cs @@ -95,7 +95,7 @@ public class MqttHandler : IMqttHandler, ITracerFeature, ILogFeature { Exchange?.Remove(Session.ID); - return null; + return new DisconnectMessage(); } /// 收到心跳时 diff --git a/NewLife.MQTT/MqttServer.cs b/NewLife.MQTT/MqttServer.cs index 8639455..9f38a8a 100644 --- a/NewLife.MQTT/MqttServer.cs +++ b/NewLife.MQTT/MqttServer.cs @@ -1,5 +1,4 @@ -using System.Net; -using NewLife.Data; +using NewLife.Data; using NewLife.Log; using NewLife.Model; using NewLife.MQTT.Clusters; @@ -209,6 +208,12 @@ protected override void OnReceive(ReceivedEventArgs e) // 父级 OnReceive 触发事件,调用 NetServer.OnReceive base.OnReceive(e); - if (msg != null && msg.Type == MqttType.Disconnect) Dispose(); + if (msg != null && msg.Type == MqttType.Disconnect) + { + // 等一会,等断开响应到达客户端 + Thread.Sleep(500); + + Dispose(); + } } } \ No newline at end of file diff --git a/NewLife.MQTT/NewLife.MQTT.csproj b/NewLife.MQTT/NewLife.MQTT.csproj index cfd90a0..36aba60 100644 --- a/NewLife.MQTT/NewLife.MQTT.csproj +++ b/NewLife.MQTT/NewLife.MQTT.csproj @@ -68,7 +68,7 @@ - + diff --git a/NewLife.MqttServer/NewLife.MqttServer.csproj b/NewLife.MqttServer/NewLife.MqttServer.csproj index 15334c1..d1b982e 100644 --- a/NewLife.MqttServer/NewLife.MqttServer.csproj +++ b/NewLife.MqttServer/NewLife.MqttServer.csproj @@ -22,7 +22,7 @@ - + diff --git a/XUnitTestClient/AliyunMqttClientTests.cs b/XUnitTestClient/AliyunMqttClientTests.cs index 302b6ec..686f9c8 100644 --- a/XUnitTestClient/AliyunMqttClientTests.cs +++ b/XUnitTestClient/AliyunMqttClientTests.cs @@ -1,48 +1,45 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Threading.Tasks; using NewLife.Log; using NewLife.MQTT; using NewLife.Security; using Xunit; -namespace XUnitTestClient +namespace XUnitTestClient; + +public class AliyunMqttClientTests { - public class AliyunMqttClientTests + [Fact] + public async Task Test3() { - [Fact] - public async void Test3() - { - var client = new AliyunMqttClient("a18RQ72tLHD", "dev1", "6oSl3CjHKM13J50DVVWNF3WbWWJjhAUf"); - client.Log = XTrace.Log; - client.Server = $"tcp://{client.ProductKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com:443"; + var client = new AliyunMqttClient("a18RQ72tLHD", "dev1", "6oSl3CjHKM13J50DVVWNF3WbWWJjhAUf"); + client.Log = XTrace.Log; + client.Server = $"tcp://{client.ProductKey}.iot-as-mqtt.cn-shanghai.aliyuncs.com:443"; - await client.ConnectAsync(); - await client.SyncTime(); - await client.PostProperty(new + await client.ConnectAsync(); + await client.SyncTime(); + await client.PostProperty(new + { + // 温度 + Temperature = Rand.Next(-4000, 120_00) / 100d, + // 相对湿度 + RelativeHumidity = Rand.Next(0, 100_00) / 100d, + // 风向 + WindDirection = Rand.Next(0, 360_00) / 100d, + // 氟化物浓度 + Fluoride = Rand.Next(0, 10000_00) / 100d, + // 空气质量指数 + AQI = Rand.Next(0, 500), + // 首要污染物 + PrimaryItem = Rand.NextString(32), + // 地理位置 + GeoLocation = new { - // 温度 - Temperature = Rand.Next(-4000, 120_00) / 100d, - // 相对湿度 - RelativeHumidity = Rand.Next(0, 100_00) / 100d, - // 风向 - WindDirection = Rand.Next(0, 360_00) / 100d, - // 氟化物浓度 - Fluoride = Rand.Next(0, 10000_00) / 100d, - // 空气质量指数 - AQI = Rand.Next(0, 500), - // 首要污染物 - PrimaryItem = Rand.NextString(32), - // 地理位置 - GeoLocation = new - { - Longitude = Rand.Next(-180_00, 180_00) / 100d, - Latitude = Rand.Next(-180_00, 180_00) / 100d, - Altitude = Rand.Next(0, 10000_00) / 100d, - // 1=WGS_84, 2=GCJ_02 - CoordinateSystem = 1, - }, - }); - } + Longitude = Rand.Next(-180_00, 180_00) / 100d, + Latitude = Rand.Next(-180_00, 180_00) / 100d, + Altitude = Rand.Next(0, 10000_00) / 100d, + // 1=WGS_84, 2=GCJ_02 + CoordinateSystem = 1, + }, + }); } } diff --git a/XUnitTestClient/Clusters/ClusterServerTests.cs b/XUnitTestClient/Clusters/ClusterServerTests.cs index 813674d..1cd646d 100644 --- a/XUnitTestClient/Clusters/ClusterServerTests.cs +++ b/XUnitTestClient/Clusters/ClusterServerTests.cs @@ -1,6 +1,7 @@ using System.Linq; using System.Net; using System.Threading; +using System.Threading.Tasks; using NewLife; using NewLife.Log; using NewLife.Model; @@ -47,7 +48,7 @@ public void StartTest() } [Fact] - public async void JoinTest() + public async Task JoinTest() { XTrace.WriteLine(nameof(JoinTest)); diff --git a/XUnitTestClient/MqttClientTests.cs b/XUnitTestClient/MqttClientTests.cs index 62bd815..fb176b0 100644 --- a/XUnitTestClient/MqttClientTests.cs +++ b/XUnitTestClient/MqttClientTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using NewLife; using NewLife.Data; using NewLife.Log; @@ -68,7 +69,7 @@ public void TestServer() [TestOrder(1)] [Fact] - public async void TestConnect() + public async Task TestConnect() { // 连接 var rs = await _client.ConnectAsync(); @@ -79,7 +80,7 @@ public async void TestConnect() [TestOrder(2)] [Fact] - public async void TestPublish() + public async Task TestPublish() { var msg = "学无先后达者为师" + Rand.NextString(8); var rs = await _client.PublishAsync("newlifeTopic", msg); @@ -93,7 +94,7 @@ public async void TestPublish() [InlineData(QualityOfService.AtMostOnce)] [InlineData(QualityOfService.AtLeastOnce)] [InlineData(QualityOfService.ExactlyOnce)] - public async void TestPublishQos(QualityOfService qos) + public async Task TestPublishQos(QualityOfService qos) { var msg = "学无先后达者为师" + Rand.NextString(8); var rs = await _client.PublishAsync("QosTopic", msg, qos); @@ -119,7 +120,7 @@ public async void TestPublishQos(QualityOfService qos) [TestOrder(4)] [Fact] - public async void TestSubscribe() + public async Task TestSubscribe() { var rs = await _client.SubscribeAsync(new[] { "newlifeTopic", "QosTopic" }); Assert.NotNull(rs); @@ -130,7 +131,7 @@ public async void TestSubscribe() [TestOrder(5)] [Fact] - public async void TestUnsubscribe() + public async Task TestUnsubscribe() { var rs = await _client.UnsubscribeAsync(new[] { "newlifeTopic", "QosTopic" }); Assert.NotNull(rs); @@ -138,7 +139,7 @@ public async void TestUnsubscribe() [TestOrder(7)] [Fact] - public async void TestPing() + public async Task TestPing() { var rs = await _client.PingAsync(); Assert.NotNull(rs); @@ -146,7 +147,7 @@ public async void TestPing() [TestOrder(10)] [Fact] - public async void TestDisconnect() + public async Task TestDisconnect() { //await _client.ConnectAsync(); diff --git a/XUnitTestClient/XUnitTestClient.csproj b/XUnitTestClient/XUnitTestClient.csproj index 45c3034..986aab3 100644 --- a/XUnitTestClient/XUnitTestClient.csproj +++ b/XUnitTestClient/XUnitTestClient.csproj @@ -11,7 +11,7 @@ - + all