AlchemyWebSocket.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Alchemy.Classes;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Common.Implementations.WebSocket
  9. {
  10. /// <summary>
  11. /// Class AlchemyWebSocket
  12. /// </summary>
  13. public class AlchemyWebSocket : IWebSocket
  14. {
  15. /// <summary>
  16. /// The logger
  17. /// </summary>
  18. private readonly ILogger _logger;
  19. /// <summary>
  20. /// Gets or sets the web socket.
  21. /// </summary>
  22. /// <value>The web socket.</value>
  23. private UserContext UserContext { get; set; }
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="AlchemyWebSocket" /> class.
  26. /// </summary>
  27. /// <param name="context">The context.</param>
  28. /// <param name="logger">The logger.</param>
  29. /// <exception cref="System.ArgumentNullException">context</exception>
  30. public AlchemyWebSocket(UserContext context, ILogger logger)
  31. {
  32. if (context == null)
  33. {
  34. throw new ArgumentNullException("context");
  35. }
  36. _logger = logger;
  37. UserContext = context;
  38. context.SetOnDisconnect(OnDisconnected);
  39. context.SetOnReceive(OnReceive);
  40. _logger.Info("Client connected from {0}", context.ClientAddress);
  41. }
  42. /// <summary>
  43. /// The _disconnected
  44. /// </summary>
  45. private bool _disconnected = false;
  46. /// <summary>
  47. /// Gets or sets the state.
  48. /// </summary>
  49. /// <value>The state.</value>
  50. public WebSocketState State
  51. {
  52. get { return _disconnected ? WebSocketState.Closed : WebSocketState.Open; }
  53. }
  54. /// <summary>
  55. /// Called when [disconnected].
  56. /// </summary>
  57. /// <param name="context">The context.</param>
  58. private void OnDisconnected(UserContext context)
  59. {
  60. _disconnected = true;
  61. }
  62. /// <summary>
  63. /// Called when [receive].
  64. /// </summary>
  65. /// <param name="context">The context.</param>
  66. private void OnReceive(UserContext context)
  67. {
  68. if (OnReceiveDelegate != null)
  69. {
  70. var json = context.DataFrame.ToString();
  71. if (!string.IsNullOrWhiteSpace(json))
  72. {
  73. try
  74. {
  75. var bytes = Encoding.UTF8.GetBytes(json);
  76. OnReceiveDelegate(bytes);
  77. }
  78. catch (Exception ex)
  79. {
  80. _logger.ErrorException("Error processing web socket message", ex);
  81. }
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// Sends the async.
  87. /// </summary>
  88. /// <param name="bytes">The bytes.</param>
  89. /// <param name="type">The type.</param>
  90. /// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
  91. /// <param name="cancellationToken">The cancellation token.</param>
  92. /// <returns>Task.</returns>
  93. public Task SendAsync(byte[] bytes, WebSocketMessageType type, bool endOfMessage, CancellationToken cancellationToken)
  94. {
  95. return Task.Run(() => UserContext.Send(bytes));
  96. }
  97. /// <summary>
  98. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  99. /// </summary>
  100. public void Dispose()
  101. {
  102. Dispose(true);
  103. }
  104. /// <summary>
  105. /// Releases unmanaged and - optionally - managed resources.
  106. /// </summary>
  107. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  108. protected virtual void Dispose(bool dispose)
  109. {
  110. }
  111. /// <summary>
  112. /// Gets or sets the receive action.
  113. /// </summary>
  114. /// <value>The receive action.</value>
  115. public Action<byte[]> OnReceiveDelegate { get; set; }
  116. }
  117. }