Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

MqttClientOptionsBuilder.cs 3.9 KiB

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