Skip to content

Commit

Permalink
⬆ MQTTnet => 4.3.6.1152
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzaz committed Aug 1, 2024
1 parent fa49500 commit 58615b6
Show file tree
Hide file tree
Showing 22 changed files with 48 additions and 320 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ jobs:
name: Release ${{ needs.build.outputs.Version }}
artifacts: "nugetPackage/*"
token: ${{ secrets.NUGET_PACKAGE_TOKEN }}
files: sourceCode/*
6 changes: 0 additions & 6 deletions MqttNet.DependencyInjection.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MqttNet.DependencyInjection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MqttNetDI.Client", "MqttNetDI.Client\MqttNetDI.Client.csproj", "{B228A523-BA2A-43B5-B692-A0DD9BBAEE46}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MqttNetDI.Server", "MqttNetDI.Server\MqttNetDI.Server.csproj", "{48E2C9C8-7B2F-4572-8BA3-BD122CF12504}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -23,10 +21,6 @@ Global
{B228A523-BA2A-43B5-B692-A0DD9BBAEE46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B228A523-BA2A-43B5-B692-A0DD9BBAEE46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B228A523-BA2A-43B5-B692-A0DD9BBAEE46}.Release|Any CPU.Build.0 = Release|Any CPU
{48E2C9C8-7B2F-4572-8BA3-BD122CF12504}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48E2C9C8-7B2F-4572-8BA3-BD122CF12504}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48E2C9C8-7B2F-4572-8BA3-BD122CF12504}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48E2C9C8-7B2F-4572-8BA3-BD122CF12504}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
44 changes: 0 additions & 44 deletions MqttNet.DependencyInjection/AbsractServerHandler.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.25" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.25" />
<PackageReference Include="MQTTnet" Version="3.1.0" />
<PackageReference Include="MQTTnet" Version="4.3.6.1152" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MqttNetDI.Client\MqttNetDI.Client.csproj" />
<ProjectReference Include="..\MqttNetDI.Server\MqttNetDI.Server.csproj" />
</ItemGroup>
</Project>
26 changes: 0 additions & 26 deletions MqttNet.DependencyInjection/MqttServerBuilder.cs

This file was deleted.

3 changes: 2 additions & 1 deletion MqttNet.DependencyInjection/Publish/MqttClientPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Protocol;

namespace MqttNet.DependencyInjection.Publish
{
Expand All @@ -34,7 +35,7 @@ public async Task PublishAsync(string topic, string message, CancellationToken c
var applicationMessage = new MqttApplicationMessageBuilder()
.WithTopic(topic) // 主题
.WithPayload(message) // 消息
.WithAtMostOnceQoS() // qos
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtMostOnce) // qos
.WithRetainFlag() // retain
.Build();

Expand Down
33 changes: 0 additions & 33 deletions MqttNet.DependencyInjection/Publish/MqttServerPublisher.cs

This file was deleted.

3 changes: 1 addition & 2 deletions MqttNetDI.Client/HeartBeat/DynamicSubManagerService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using MQTTnet.Client.Subscribing;
using MQTTnet.Client.Unsubscribing;
using MQTTnet.Client;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
3 changes: 2 additions & 1 deletion MqttNetDI.Client/HeartBeat/HeartBeatService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using MQTTnet;
using MQTTnet.Protocol;
using MqttNetDI.Client;
using Newtonsoft.Json;
using System;
Expand Down Expand Up @@ -39,7 +40,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
var applicationMessage = new MqttApplicationMessageBuilder()
.WithTopic(_options.PubHeartBeatTopic) // 主题
.WithPayload(JsonConvert.SerializeObject(heartBeatInfo)) // 消息
.WithAtMostOnceQoS() // qos
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtMostOnce) // qos
.Build();
await _mqttClientCreate.mqttClient.PublishAsync(applicationMessage, stoppingToken);
}
Expand Down
6 changes: 1 addition & 5 deletions MqttNetDI.Client/IMqttClientCreate.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using MQTTnet.Client;
using MQTTnet.Client.Options;
using System;
using System.Collections.Generic;
using System.Text;

namespace MqttNetDI.Client
{
public interface IMqttClientCreate
{
IMqttClient mqttClient { get; }
IMqttClientOptions mqttClientOptions { get; set; }
MqttClientOptions mqttClientOptions { get; set; }
}
}
6 changes: 3 additions & 3 deletions MqttNetDI.Client/IMqttClientEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace MqttNetDI.Client
{
public interface IMqttClientEventHandler
{
void SetTopic(out List<ClientTopic> topicList);
void HeartBeatReceived(HeartBeatArgs args);
void MessageReceived(MessageReceiveArgs args);
void SetTopic(out IEnumerable<ClientTopic> topicList);
Task HeartBeatReceivedAsync(HeartBeatArgs args);
Task MessageReceivedAsync(MessageReceiveArgs args);
}
}
24 changes: 23 additions & 1 deletion MqttNetDI.Client/MqttClientConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Packets;
using MQTTnet.Protocol;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -58,13 +60,33 @@ public class MessageReceiveArgs
/// </summary>
public Action<CancellationToken> AcknowledgeAsync { get; }

public MessageReceiveArgs(string topic, string clientId, string message, MqttQualityOfServiceLevel qosLevel, bool retain, Action<CancellationToken> action)
/// <summary>
/// Gets or sets the reason code which will be sent to the server.
/// </summary>
public MqttApplicationMessageReceivedReasonCode ReasonCode { get; }

/// <summary>
/// Gets or sets the user properties which will be sent to the server in the ACK packet etc.
/// </summary>
public List<MqttUserProperty> ResponseUserProperties { get; }

public MessageReceiveArgs(
string topic,
string clientId,
string message,
MqttQualityOfServiceLevel qosLevel,
bool retain,
MqttApplicationMessageReceivedReasonCode reasonCode,
List<MqttUserProperty> userProperties,
Action<CancellationToken> action)
{
Topic = topic;
ClientId = clientId;
Message = message;
QosLevel = qosLevel;
Retain = retain;
ReasonCode = reasonCode;
ResponseUserProperties = userProperties;
AcknowledgeAsync = action;
}
}
Expand Down
4 changes: 2 additions & 2 deletions MqttNetDI.Client/MqttClientCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MqttClientCreate : IMqttClientCreate
private readonly MqttClientConfig _mqttClientConfig;

public IMqttClient mqttClient { get; set; }
public IMqttClientOptions mqttClientOptions { get; set; }
public MqttClientOptions mqttClientOptions { get; set; }
public MqttClientCreate(IOptions<MqttClientConfig> options)
{
_mqttClientConfig = options.Value;
Expand All @@ -35,7 +35,7 @@ public MqttClientCreate(IOptions<MqttClientConfig> options)
optionsBuilder.WithKeepAlivePeriod(TimeSpan.FromSeconds(2000));
optionsBuilder.WithCleanSession(true);
optionsBuilder.WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V310);
optionsBuilder.WithCommunicationTimeout(TimeSpan.FromHours(1));
optionsBuilder.WithTimeout(TimeSpan.FromHours(1));
// 创建选项
mqttClientOptions = optionsBuilder.Build();
}
Expand Down
23 changes: 11 additions & 12 deletions MqttNetDI.Client/MqttClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using Microsoft.Extensions.Options;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Options;
using MQTTnet.Client.Subscribing;
using MqttNetDI.Client.HeartBeat;
using Newtonsoft.Json;
using System;
Expand Down Expand Up @@ -42,10 +40,10 @@ public async Task ConnetAsync()
try
{
// 设置消息接收处理程序
_mqttClientCreate.mqttClient.UseApplicationMessageReceivedHandler(MessageReceived);
_mqttClientCreate.mqttClient.ApplicationMessageReceivedAsync += MessageReceivedAsync;

// 重连机制
_mqttClientCreate.mqttClient.UseDisconnectedHandler(async e =>
_mqttClientCreate.mqttClient.DisconnectedAsync += (async e =>
{
Console.WriteLine("与服务器之间的连接断开了,正在尝试重新连接");
// 等待 5s 时间
Expand Down Expand Up @@ -81,11 +79,11 @@ public async Task ConnetAsync()
}
}

private void MessageReceived(MqttApplicationMessageReceivedEventArgs args)
private async Task MessageReceivedAsync(MqttApplicationMessageReceivedEventArgs args)
{
string topic = args.ApplicationMessage.Topic;
string ClientId = args.ClientId;
string payload = Encoding.UTF8.GetString(args.ApplicationMessage.Payload);
string payload = Encoding.UTF8.GetString(args.ApplicationMessage.PayloadSegment);
if (topic == _options.SubcribeHeartBeatTopic)
{
HeartBeatArgs heartBeatInfo = JsonConvert.DeserializeObject<HeartBeatArgs>(payload);
Expand All @@ -102,7 +100,7 @@ private void MessageReceived(MqttApplicationMessageReceivedEventArgs args)
HeartBeatinterval = heartBeatInfo.HeartBeatinterval,
});
}
_mqttClientEventHandler.HeartBeatReceived(heartBeatInfo);
await _mqttClientEventHandler.HeartBeatReceivedAsync(heartBeatInfo);
return;
}
MessageReceiveArgs messageReceiveArgs = new MessageReceiveArgs(
Expand All @@ -111,15 +109,16 @@ private void MessageReceived(MqttApplicationMessageReceivedEventArgs args)
payload,
args.ApplicationMessage.QualityOfServiceLevel,
args.ApplicationMessage.Retain,
(CancellationToken) => { args.AcknowledgeAsync(CancellationToken); });
_mqttClientEventHandler.MessageReceived(messageReceiveArgs);
args.ReasonCode,
args.ResponseUserProperties,
async (CancellationToken) => { await args.AcknowledgeAsync(CancellationToken); });
await _mqttClientEventHandler.MessageReceivedAsync(messageReceiveArgs);
}

private async Task SubScribe()
{
List<ClientTopic> clientTopics = new List<ClientTopic>();
_mqttClientEventHandler.SetTopic(out clientTopics);
List<string> topiclist = new List<string>();
_mqttClientEventHandler.SetTopic(out var clientTopics);
var topiclist = new List<string>();
foreach (var item in clientTopics)
{
topiclist.AddRange(item.TopicList);
Expand Down
2 changes: 1 addition & 1 deletion MqttNetDI.Client/MqttNetDI.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.25" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.25" />
<PackageReference Include="MQTTnet" Version="3.1.0" />
<PackageReference Include="MQTTnet" Version="4.3.6.1152" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
14 changes: 0 additions & 14 deletions MqttNetDI.Server/IMqttServerCreate.cs

This file was deleted.

17 changes: 0 additions & 17 deletions MqttNetDI.Server/IMqttServerEventHandler.cs

This file was deleted.

11 changes: 0 additions & 11 deletions MqttNetDI.Server/IMqttServerService.cs

This file was deleted.

Loading

0 comments on commit 58615b6

Please sign in to comment.