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.
 
 
 
 

136 lines
3.8 KiB

  1. using System;
  2. using System.Linq;
  3. using MQTTnet.Serializer;
  4. namespace MQTTnet.Client
  5. {
  6. public class MqttClientOptionsBuilder
  7. {
  8. private readonly MqttClientOptions _options = new MqttClientOptions();
  9. private MqttClientTcpOptions _tcpOptions;
  10. private MqttClientWebSocketOptions _webSocketOptions;
  11. private MqttClientTlsOptions _tlsOptions;
  12. public MqttClientOptionsBuilder WithProtocolVersion(MqttProtocolVersion value)
  13. {
  14. _options.ProtocolVersion = value;
  15. return this;
  16. }
  17. public MqttClientOptionsBuilder WithCommunicationTimeout(TimeSpan value)
  18. {
  19. _options.CommunicationTimeout = value;
  20. return this;
  21. }
  22. public MqttClientOptionsBuilder WithCleanSession(bool value = true)
  23. {
  24. _options.CleanSession = value;
  25. return this;
  26. }
  27. public MqttClientOptionsBuilder WithKeepAlivePeriod(TimeSpan value)
  28. {
  29. _options.KeepAlivePeriod = value;
  30. return this;
  31. }
  32. public MqttClientOptionsBuilder WithClientId(string value)
  33. {
  34. _options.ClientId = value;
  35. return this;
  36. }
  37. public MqttClientOptionsBuilder WithWillMessage(MqttApplicationMessage value)
  38. {
  39. _options.WillMessage = value;
  40. return this;
  41. }
  42. public MqttClientOptionsBuilder WithCredentials(string username, string password = null)
  43. {
  44. _options.Credentials = new MqttClientCredentials
  45. {
  46. Username = username,
  47. Password = password
  48. };
  49. return this;
  50. }
  51. public MqttClientOptionsBuilder WithTcpServer(string server, int? port = null)
  52. {
  53. _tcpOptions = new MqttClientTcpOptions
  54. {
  55. Server = server,
  56. Port = port
  57. };
  58. return this;
  59. }
  60. public MqttClientOptionsBuilder WithWebSocketServer(string uri)
  61. {
  62. _webSocketOptions = new MqttClientWebSocketOptions
  63. {
  64. Uri = uri
  65. };
  66. return this;
  67. }
  68. public MqttClientOptionsBuilder WithTls(
  69. bool allowUntrustedCertificates = false,
  70. bool ignoreCertificateChainErrors = false,
  71. bool ignoreCertificateRevocationErrors = false,
  72. params byte[][] certificates)
  73. {
  74. _tlsOptions = new MqttClientTlsOptions
  75. {
  76. UseTls = true,
  77. AllowUntrustedCertificates = allowUntrustedCertificates,
  78. IgnoreCertificateChainErrors = ignoreCertificateChainErrors,
  79. IgnoreCertificateRevocationErrors = ignoreCertificateRevocationErrors,
  80. Certificates = certificates?.ToList()
  81. };
  82. return this;
  83. }
  84. public MqttClientOptionsBuilder WithTls()
  85. {
  86. _tlsOptions = new MqttClientTlsOptions
  87. {
  88. UseTls = true
  89. };
  90. return this;
  91. }
  92. public IMqttClientOptions Build()
  93. {
  94. if (_tlsOptions != null)
  95. {
  96. if (_tcpOptions == null && _webSocketOptions == null)
  97. {
  98. throw new InvalidOperationException("A channel (TCP or WebSocket) must be set if TLS is configured.");
  99. }
  100. if (_tcpOptions != null)
  101. {
  102. _options.ChannelOptions = _tcpOptions;
  103. }
  104. else
  105. {
  106. _options.ChannelOptions = _webSocketOptions;
  107. }
  108. }
  109. _options.ChannelOptions = (IMqttClientChannelOptions)_tcpOptions ?? _webSocketOptions;
  110. return _options;
  111. }
  112. }
  113. }