WebSocketController.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Session;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Net;
  6. using MediaBrowser.Model.Session;
  7. using MediaBrowser.Model.System;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Server.Implementations.Session
  14. {
  15. public class WebSocketController : ISessionController
  16. {
  17. public SessionInfo Session { get; private set; }
  18. public List<IWebSocketConnection> Sockets { get; private set; }
  19. private readonly IServerApplicationHost _appHost;
  20. public WebSocketController(SessionInfo session, IServerApplicationHost appHost)
  21. {
  22. Session = session;
  23. _appHost = appHost;
  24. Sockets = new List<IWebSocketConnection>();
  25. }
  26. public bool SupportsMediaRemoteControl
  27. {
  28. get
  29. {
  30. return Sockets.Any(i => i.State == WebSocketState.Open);
  31. }
  32. }
  33. public bool IsSessionActive
  34. {
  35. get
  36. {
  37. return Sockets.Any(i => i.State == WebSocketState.Open);
  38. }
  39. }
  40. private IWebSocketConnection GetActiveSocket()
  41. {
  42. var socket = Sockets
  43. .OrderByDescending(i => i.LastActivityDate)
  44. .FirstOrDefault(i => i.State == WebSocketState.Open);
  45. if (socket == null)
  46. {
  47. throw new InvalidOperationException("The requested session does not have an open web socket.");
  48. }
  49. return socket;
  50. }
  51. public Task SendMessageCommand(MessageCommand command, CancellationToken cancellationToken)
  52. {
  53. var socket = GetActiveSocket();
  54. return socket.SendAsync(new WebSocketMessage<MessageCommand>
  55. {
  56. MessageType = "MessageCommand",
  57. Data = command
  58. }, cancellationToken);
  59. }
  60. public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
  61. {
  62. var socket = GetActiveSocket();
  63. return socket.SendAsync(new WebSocketMessage<PlayRequest>
  64. {
  65. MessageType = "Play",
  66. Data = command
  67. }, cancellationToken);
  68. }
  69. public Task SendBrowseCommand(BrowseRequest command, CancellationToken cancellationToken)
  70. {
  71. var socket = GetActiveSocket();
  72. return socket.SendAsync(new WebSocketMessage<BrowseRequest>
  73. {
  74. MessageType = "Browse",
  75. Data = command
  76. }, cancellationToken);
  77. }
  78. public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
  79. {
  80. var socket = GetActiveSocket();
  81. return socket.SendAsync(new WebSocketMessage<PlaystateRequest>
  82. {
  83. MessageType = "Playstate",
  84. Data = command
  85. }, cancellationToken);
  86. }
  87. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  88. {
  89. var socket = GetActiveSocket();
  90. return socket.SendAsync(new WebSocketMessage<LibraryUpdateInfo>
  91. {
  92. MessageType = "LibraryChanged",
  93. Data = info
  94. }, cancellationToken);
  95. }
  96. /// <summary>
  97. /// Sends the restart required message.
  98. /// </summary>
  99. /// <param name="cancellationToken">The cancellation token.</param>
  100. /// <returns>Task.</returns>
  101. public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
  102. {
  103. var socket = GetActiveSocket();
  104. return socket.SendAsync(new WebSocketMessage<SystemInfo>
  105. {
  106. MessageType = "RestartRequired",
  107. Data = _appHost.GetSystemInfo()
  108. }, cancellationToken);
  109. }
  110. /// <summary>
  111. /// Sends the user data change info.
  112. /// </summary>
  113. /// <param name="info">The info.</param>
  114. /// <param name="cancellationToken">The cancellation token.</param>
  115. /// <returns>Task.</returns>
  116. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  117. {
  118. var socket = GetActiveSocket();
  119. return socket.SendAsync(new WebSocketMessage<UserDataChangeInfo>
  120. {
  121. MessageType = "UserDataChanged",
  122. Data = info
  123. }, cancellationToken);
  124. }
  125. /// <summary>
  126. /// Sends the server shutdown notification.
  127. /// </summary>
  128. /// <param name="cancellationToken">The cancellation token.</param>
  129. /// <returns>Task.</returns>
  130. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  131. {
  132. var socket = GetActiveSocket();
  133. return socket.SendAsync(new WebSocketMessage<string>
  134. {
  135. MessageType = "ServerShuttingDown",
  136. Data = string.Empty
  137. }, cancellationToken);
  138. }
  139. /// <summary>
  140. /// Sends the server restart notification.
  141. /// </summary>
  142. /// <param name="cancellationToken">The cancellation token.</param>
  143. /// <returns>Task.</returns>
  144. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  145. {
  146. var socket = GetActiveSocket();
  147. return socket.SendAsync(new WebSocketMessage<string>
  148. {
  149. MessageType = "ServerRestarting",
  150. Data = string.Empty
  151. }, cancellationToken);
  152. }
  153. public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
  154. {
  155. var socket = GetActiveSocket();
  156. return socket.SendAsync(new WebSocketMessage<GeneralCommand>
  157. {
  158. MessageType = "GeneralCommand",
  159. Data = command
  160. }, cancellationToken);
  161. }
  162. }
  163. }