Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MqttWebSocketChannel.cs 2.8 KiB

il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
il y a 7 ans
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using MQTTnet.Core.Channel;
  2. using MQTTnet.Core.Client;
  3. using System;
  4. using System.IO;
  5. using System.Net.WebSockets;
  6. using System.Security.Cryptography.X509Certificates;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MQTTnet.Implementations
  10. {
  11. public sealed class MqttWebSocketChannel : IMqttCommunicationChannel, IDisposable
  12. {
  13. private readonly MqttClientWebSocketOptions _options;
  14. private ClientWebSocket _webSocket;
  15. public MqttWebSocketChannel(MqttClientWebSocketOptions options)
  16. {
  17. _options = options ?? throw new ArgumentNullException(nameof(options));
  18. }
  19. public Stream SendStream => RawReceiveStream;
  20. public Stream ReceiveStream => RawReceiveStream;
  21. public Stream RawReceiveStream { get; private set; }
  22. public async Task ConnectAsync()
  23. {
  24. var uri = _options.Uri;
  25. if (!uri.StartsWith("ws://", StringComparison.OrdinalIgnoreCase))
  26. {
  27. uri = "ws://" + uri;
  28. }
  29. _webSocket = new ClientWebSocket();
  30. _webSocket.Options.KeepAliveInterval = _options.KeepAlivePeriod;
  31. if (_options.RequestHeaders != null)
  32. {
  33. foreach (var requestHeader in _options.RequestHeaders)
  34. {
  35. _webSocket.Options.SetRequestHeader(requestHeader.Key, requestHeader.Value);
  36. }
  37. }
  38. if (_options.SubProtocols != null)
  39. {
  40. foreach (var subProtocol in _options.SubProtocols)
  41. {
  42. _webSocket.Options.AddSubProtocol(subProtocol);
  43. }
  44. }
  45. if (_options.CookieContainer != null)
  46. {
  47. _webSocket.Options.Cookies = _options.CookieContainer;
  48. }
  49. if (_options.TlsOptions?.UseTls == true && _options.TlsOptions?.Certificates != null)
  50. {
  51. _webSocket.Options.ClientCertificates = new X509CertificateCollection();
  52. foreach (var certificate in _options.TlsOptions.Certificates)
  53. {
  54. _webSocket.Options.ClientCertificates.Add(new X509Certificate(certificate));
  55. }
  56. }
  57. await _webSocket.ConnectAsync(new Uri(uri), CancellationToken.None);
  58. RawReceiveStream = new WebSocketStream(_webSocket);
  59. }
  60. public async Task DisconnectAsync()
  61. {
  62. RawReceiveStream = null;
  63. if (_webSocket == null)
  64. {
  65. return;
  66. }
  67. await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).ConfigureAwait(false);
  68. }
  69. public void Dispose()
  70. {
  71. _webSocket?.Dispose();
  72. }
  73. }
  74. }