IWebSocketConnection.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace MediaBrowser.Common.Net
  5. {
  6. public interface IWebSocketConnection : IDisposable
  7. {
  8. /// <summary>
  9. /// Gets or sets the receive action.
  10. /// </summary>
  11. /// <value>The receive action.</value>
  12. Action<WebSocketMessageInfo> OnReceive { get; set; }
  13. /// <summary>
  14. /// Gets the state.
  15. /// </summary>
  16. /// <value>The state.</value>
  17. WebSocketState State { get; }
  18. /// <summary>
  19. /// Gets the remote end point.
  20. /// </summary>
  21. /// <value>The remote end point.</value>
  22. string RemoteEndPoint { get; }
  23. /// <summary>
  24. /// Sends a message asynchronously.
  25. /// </summary>
  26. /// <typeparam name="T"></typeparam>
  27. /// <param name="message">The message.</param>
  28. /// <param name="cancellationToken">The cancellation token.</param>
  29. /// <returns>Task.</returns>
  30. /// <exception cref="System.ArgumentNullException">message</exception>
  31. Task SendAsync<T>(WebSocketMessage<T> message, CancellationToken cancellationToken);
  32. /// <summary>
  33. /// Sends a message asynchronously.
  34. /// </summary>
  35. /// <param name="buffer">The buffer.</param>
  36. /// <param name="cancellationToken">The cancellation token.</param>
  37. /// <returns>Task.</returns>
  38. Task SendAsync(byte[] buffer, CancellationToken cancellationToken);
  39. /// <summary>
  40. /// Sends a message asynchronously.
  41. /// </summary>
  42. /// <param name="buffer">The buffer.</param>
  43. /// <param name="type">The type.</param>
  44. /// <param name="cancellationToken">The cancellation token.</param>
  45. /// <returns>Task.</returns>
  46. /// <exception cref="System.ArgumentNullException">buffer</exception>
  47. Task SendAsync(byte[] buffer, WebSocketMessageType type, CancellationToken cancellationToken);
  48. }
  49. /// <summary>
  50. /// Class WebSocketMessage
  51. /// </summary>
  52. /// <typeparam name="T"></typeparam>
  53. public class WebSocketMessage<T>
  54. {
  55. /// <summary>
  56. /// Gets or sets the type of the message.
  57. /// </summary>
  58. /// <value>The type of the message.</value>
  59. public string MessageType { get; set; }
  60. /// <summary>
  61. /// Gets or sets the data.
  62. /// </summary>
  63. /// <value>The data.</value>
  64. public T Data { get; set; }
  65. }
  66. /// <summary>
  67. /// Class WebSocketMessageInfo
  68. /// </summary>
  69. public class WebSocketMessageInfo : WebSocketMessage<string>
  70. {
  71. /// <summary>
  72. /// Gets or sets the connection.
  73. /// </summary>
  74. /// <value>The connection.</value>
  75. public IWebSocketConnection Connection { get; set; }
  76. }
  77. }