AlchemyWebSocket.cs 4.1 KB

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