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.

MqttWebSocketChannel.cs 3.5 KiB

7 vuotta sitten
7 vuotta sitten
7 vuotta sitten
7 vuotta sitten
7 vuotta sitten
7 vuotta sitten
7 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.IO;
  3. using System.Net.WebSockets;
  4. using System.Security.Cryptography.X509Certificates;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MQTTnet.Channel;
  8. using MQTTnet.Client;
  9. namespace MQTTnet.Implementations
  10. {
  11. public sealed class MqttWebSocketChannel : IMqttChannel
  12. {
  13. // ReSharper disable once MemberCanBePrivate.Global
  14. // ReSharper disable once AutoPropertyCanBeMadeGetOnly.Global
  15. public static int BufferSize { get; set; } = 4096 * 20; // Can be changed for fine tuning by library user.
  16. private readonly MqttClientWebSocketOptions _options;
  17. private ClientWebSocket _webSocket;
  18. public MqttWebSocketChannel(MqttClientWebSocketOptions options)
  19. {
  20. _options = options ?? throw new ArgumentNullException(nameof(options));
  21. }
  22. public Stream SendStream { get; private set; }
  23. public Stream ReceiveStream { get; private set; }
  24. public async Task ConnectAsync()
  25. {
  26. var uri = _options.Uri;
  27. if (!uri.StartsWith("ws://", StringComparison.OrdinalIgnoreCase) && !uri.StartsWith("wss://", StringComparison.OrdinalIgnoreCase))
  28. {
  29. if (_options.TlsOptions?.UseTls == false)
  30. {
  31. uri = "ws://" + uri;
  32. }
  33. else
  34. {
  35. uri = "wss://" + uri;
  36. }
  37. }
  38. _webSocket = new ClientWebSocket();
  39. if (_options.RequestHeaders != null)
  40. {
  41. foreach (var requestHeader in _options.RequestHeaders)
  42. {
  43. _webSocket.Options.SetRequestHeader(requestHeader.Key, requestHeader.Value);
  44. }
  45. }
  46. if (_options.SubProtocols != null)
  47. {
  48. foreach (var subProtocol in _options.SubProtocols)
  49. {
  50. _webSocket.Options.AddSubProtocol(subProtocol);
  51. }
  52. }
  53. if (_options.CookieContainer != null)
  54. {
  55. _webSocket.Options.Cookies = _options.CookieContainer;
  56. }
  57. if (_options.TlsOptions?.UseTls == true && _options.TlsOptions?.Certificates != null)
  58. {
  59. _webSocket.Options.ClientCertificates = new X509CertificateCollection();
  60. foreach (var certificate in _options.TlsOptions.Certificates)
  61. {
  62. _webSocket.Options.ClientCertificates.Add(new X509Certificate(certificate));
  63. }
  64. }
  65. await _webSocket.ConnectAsync(new Uri(uri), CancellationToken.None).ConfigureAwait(false);
  66. SendStream = new WebSocketStream(_webSocket);
  67. ReceiveStream = SendStream;
  68. }
  69. public async Task DisconnectAsync()
  70. {
  71. if (_webSocket == null)
  72. {
  73. return;
  74. }
  75. if (_webSocket.State == WebSocketState.Open || _webSocket.State == WebSocketState.Connecting)
  76. {
  77. await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).ConfigureAwait(false);
  78. }
  79. Dispose();
  80. }
  81. public void Dispose()
  82. {
  83. try
  84. {
  85. _webSocket?.Dispose();
  86. }
  87. catch (ObjectDisposedException)
  88. {
  89. }
  90. finally
  91. {
  92. _webSocket = null;
  93. }
  94. }
  95. }
  96. }