WebSocketController.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
  52. {
  53. var socket = GetActiveSocket();
  54. return socket.SendAsync(new WebSocketMessage<PlayRequest>
  55. {
  56. MessageType = "Play",
  57. Data = command
  58. }, cancellationToken);
  59. }
  60. public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
  61. {
  62. var socket = GetActiveSocket();
  63. return socket.SendAsync(new WebSocketMessage<PlaystateRequest>
  64. {
  65. MessageType = "Playstate",
  66. Data = command
  67. }, cancellationToken);
  68. }
  69. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  70. {
  71. var socket = GetActiveSocket();
  72. return socket.SendAsync(new WebSocketMessage<LibraryUpdateInfo>
  73. {
  74. MessageType = "LibraryChanged",
  75. Data = info
  76. }, cancellationToken);
  77. }
  78. /// <summary>
  79. /// Sends the restart required message.
  80. /// </summary>
  81. /// <param name="cancellationToken">The cancellation token.</param>
  82. /// <returns>Task.</returns>
  83. public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
  84. {
  85. var socket = GetActiveSocket();
  86. return socket.SendAsync(new WebSocketMessage<SystemInfo>
  87. {
  88. MessageType = "RestartRequired",
  89. Data = _appHost.GetSystemInfo()
  90. }, cancellationToken);
  91. }
  92. /// <summary>
  93. /// Sends the user data change info.
  94. /// </summary>
  95. /// <param name="info">The info.</param>
  96. /// <param name="cancellationToken">The cancellation token.</param>
  97. /// <returns>Task.</returns>
  98. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  99. {
  100. var socket = GetActiveSocket();
  101. return socket.SendAsync(new WebSocketMessage<UserDataChangeInfo>
  102. {
  103. MessageType = "UserDataChanged",
  104. Data = info
  105. }, cancellationToken);
  106. }
  107. /// <summary>
  108. /// Sends the server shutdown notification.
  109. /// </summary>
  110. /// <param name="cancellationToken">The cancellation token.</param>
  111. /// <returns>Task.</returns>
  112. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  113. {
  114. var socket = GetActiveSocket();
  115. return socket.SendAsync(new WebSocketMessage<string>
  116. {
  117. MessageType = "ServerShuttingDown",
  118. Data = string.Empty
  119. }, cancellationToken);
  120. }
  121. /// <summary>
  122. /// Sends the server restart notification.
  123. /// </summary>
  124. /// <param name="cancellationToken">The cancellation token.</param>
  125. /// <returns>Task.</returns>
  126. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  127. {
  128. var socket = GetActiveSocket();
  129. return socket.SendAsync(new WebSocketMessage<string>
  130. {
  131. MessageType = "ServerRestarting",
  132. Data = string.Empty
  133. }, cancellationToken);
  134. }
  135. public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
  136. {
  137. var socket = GetActiveSocket();
  138. return socket.SendAsync(new WebSocketMessage<GeneralCommand>
  139. {
  140. MessageType = "GeneralCommand",
  141. Data = command
  142. }, cancellationToken);
  143. }
  144. public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  145. {
  146. var socket = GetActiveSocket();
  147. return socket.SendAsync(new WebSocketMessage<SessionInfoDto>
  148. {
  149. MessageType = "SessionEnded",
  150. Data = sessionInfo
  151. }, cancellationToken);
  152. }
  153. public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  154. {
  155. var socket = GetActiveSocket();
  156. return socket.SendAsync(new WebSocketMessage<SessionInfoDto>
  157. {
  158. MessageType = "PlaybackStart",
  159. Data = sessionInfo
  160. }, cancellationToken);
  161. }
  162. public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  163. {
  164. var socket = GetActiveSocket();
  165. return socket.SendAsync(new WebSocketMessage<SessionInfoDto>
  166. {
  167. MessageType = "PlaybackStopped",
  168. Data = sessionInfo
  169. }, cancellationToken);
  170. }
  171. }
  172. }