You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MqttClientConnectionContextFactory.cs 1.2 KiB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Net;
  3. using MQTTnet.Adapter;
  4. using MQTTnet.AspNetCore.Client.Tcp;
  5. using MQTTnet.Client;
  6. using MQTTnet.Client.Options;
  7. using MQTTnet.Diagnostics;
  8. using MQTTnet.Formatter;
  9. namespace MQTTnet.AspNetCore.Client
  10. {
  11. public class MqttClientConnectionContextFactory : IMqttClientAdapterFactory
  12. {
  13. public IMqttChannelAdapter CreateClientAdapter(IMqttClientOptions options, IMqttNetChildLogger logger)
  14. {
  15. if (options == null) throw new ArgumentNullException(nameof(options));
  16. switch (options.ChannelOptions)
  17. {
  18. case MqttClientTcpOptions tcpOptions:
  19. {
  20. var endpoint = new DnsEndPoint(tcpOptions.Server, tcpOptions.GetPort());
  21. var tcpConnection = new TcpConnection(endpoint);
  22. var writer = new SpanBasedMqttPacketWriter();
  23. var formatter = new MqttPacketFormatterAdapter(options.ProtocolVersion, writer);
  24. return new MqttConnectionContext(formatter, tcpConnection);
  25. }
  26. default:
  27. {
  28. throw new NotSupportedException();
  29. }
  30. }
  31. }
  32. }
  33. }