WebSocketController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 IsSessionActive
  27. {
  28. get
  29. {
  30. return Sockets.Any(i => i.State == WebSocketState.Open);
  31. }
  32. }
  33. private IWebSocketConnection GetActiveSocket()
  34. {
  35. var socket = Sockets
  36. .OrderByDescending(i => i.LastActivityDate)
  37. .FirstOrDefault(i => i.State == WebSocketState.Open);
  38. if (socket == null)
  39. {
  40. throw new InvalidOperationException("The requested session does not have an open web socket.");
  41. }
  42. return socket;
  43. }
  44. public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
  45. {
  46. var socket = GetActiveSocket();
  47. return socket.SendAsync(new WebSocketMessage<PlayRequest>
  48. {
  49. MessageType = "Play",
  50. Data = command
  51. }, cancellationToken);
  52. }
  53. public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
  54. {
  55. var socket = GetActiveSocket();
  56. return socket.SendAsync(new WebSocketMessage<PlaystateRequest>
  57. {
  58. MessageType = "Playstate",
  59. Data = command
  60. }, cancellationToken);
  61. }
  62. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  63. {
  64. var socket = GetActiveSocket();
  65. return socket.SendAsync(new WebSocketMessage<LibraryUpdateInfo>
  66. {
  67. MessageType = "LibraryChanged",
  68. Data = info
  69. }, cancellationToken);
  70. }
  71. /// <summary>
  72. /// Sends the restart required message.
  73. /// </summary>
  74. /// <param name="cancellationToken">The cancellation token.</param>
  75. /// <returns>Task.</returns>
  76. public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
  77. {
  78. var socket = GetActiveSocket();
  79. return socket.SendAsync(new WebSocketMessage<SystemInfo>
  80. {
  81. MessageType = "RestartRequired",
  82. Data = _appHost.GetSystemInfo()
  83. }, cancellationToken);
  84. }
  85. /// <summary>
  86. /// Sends the user data change info.
  87. /// </summary>
  88. /// <param name="info">The info.</param>
  89. /// <param name="cancellationToken">The cancellation token.</param>
  90. /// <returns>Task.</returns>
  91. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  92. {
  93. var socket = GetActiveSocket();
  94. return socket.SendAsync(new WebSocketMessage<UserDataChangeInfo>
  95. {
  96. MessageType = "UserDataChanged",
  97. Data = info
  98. }, cancellationToken);
  99. }
  100. /// <summary>
  101. /// Sends the server shutdown notification.
  102. /// </summary>
  103. /// <param name="cancellationToken">The cancellation token.</param>
  104. /// <returns>Task.</returns>
  105. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  106. {
  107. var socket = GetActiveSocket();
  108. return socket.SendAsync(new WebSocketMessage<string>
  109. {
  110. MessageType = "ServerShuttingDown",
  111. Data = string.Empty
  112. }, cancellationToken);
  113. }
  114. /// <summary>
  115. /// Sends the server restart notification.
  116. /// </summary>
  117. /// <param name="cancellationToken">The cancellation token.</param>
  118. /// <returns>Task.</returns>
  119. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  120. {
  121. var socket = GetActiveSocket();
  122. return socket.SendAsync(new WebSocketMessage<string>
  123. {
  124. MessageType = "ServerRestarting",
  125. Data = string.Empty
  126. }, cancellationToken);
  127. }
  128. public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
  129. {
  130. var socket = GetActiveSocket();
  131. return socket.SendAsync(new WebSocketMessage<GeneralCommand>
  132. {
  133. MessageType = "GeneralCommand",
  134. Data = command
  135. }, cancellationToken);
  136. }
  137. public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  138. {
  139. var socket = GetActiveSocket();
  140. return socket.SendAsync(new WebSocketMessage<SessionInfoDto>
  141. {
  142. MessageType = "SessionEnded",
  143. Data = sessionInfo
  144. }, cancellationToken);
  145. }
  146. public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  147. {
  148. var socket = GetActiveSocket();
  149. return socket.SendAsync(new WebSocketMessage<SessionInfoDto>
  150. {
  151. MessageType = "PlaybackStart",
  152. Data = sessionInfo
  153. }, cancellationToken);
  154. }
  155. public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  156. {
  157. var socket = GetActiveSocket();
  158. return socket.SendAsync(new WebSocketMessage<SessionInfoDto>
  159. {
  160. MessageType = "PlaybackStopped",
  161. Data = sessionInfo
  162. }, cancellationToken);
  163. }
  164. }
  165. }