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

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