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.
 
 
 
 

35 lines
1.2 KiB

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