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.
 
 
 
 

43 lines
1.4 KiB

  1. using MQTTnet.Adapter;
  2. using MQTTnet.Client.Options;
  3. using MQTTnet.Diagnostics;
  4. using MQTTnet.Formatter;
  5. using MQTTnet.Implementations;
  6. using System;
  7. namespace MQTTnet.Extensions.WebSocket4Net
  8. {
  9. public class WebSocket4NetMqttClientAdapterFactory : IMqttClientAdapterFactory
  10. {
  11. readonly IMqttNetLogger _logger;
  12. public WebSocket4NetMqttClientAdapterFactory(IMqttNetLogger logger)
  13. {
  14. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  15. }
  16. public IMqttChannelAdapter CreateClientAdapter(IMqttClientOptions options)
  17. {
  18. if (options == null) throw new ArgumentNullException(nameof(options));
  19. switch (options.ChannelOptions)
  20. {
  21. case MqttClientTcpOptions _:
  22. {
  23. return new MqttChannelAdapter(new MqttTcpChannel(options), new MqttPacketFormatterAdapter(options.ProtocolVersion), _logger);
  24. }
  25. case MqttClientWebSocketOptions webSocketOptions:
  26. {
  27. return new MqttChannelAdapter(new WebSocket4NetMqttChannel(options, webSocketOptions), new MqttPacketFormatterAdapter(options.ProtocolVersion), _logger);
  28. }
  29. default:
  30. {
  31. throw new NotSupportedException();
  32. }
  33. }
  34. }
  35. }
  36. }