No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

103 líneas
3.3 KiB

  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, IDisposable
  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. _webSocket?.Dispose();
  84. _webSocket = null;
  85. }
  86. }
  87. }