AlchemyWebSocket.cs 4.2 KB

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