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