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.
 
 
 
 

51 rivejä
1.6 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 sealed 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(
  24. new MqttTcpChannel(options),
  25. new MqttPacketFormatterAdapter(options.ProtocolVersion),
  26. options.PacketInspector,
  27. _logger);
  28. }
  29. case MqttClientWebSocketOptions webSocketOptions:
  30. {
  31. return new MqttChannelAdapter(
  32. new WebSocket4NetMqttChannel(options, webSocketOptions),
  33. new MqttPacketFormatterAdapter(options.ProtocolVersion),
  34. options.PacketInspector,
  35. _logger);
  36. }
  37. default:
  38. {
  39. throw new NotSupportedException();
  40. }
  41. }
  42. }
  43. }
  44. }