HttpSessionController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.Serialization;
  7. using MediaBrowser.Model.Session;
  8. using MediaBrowser.Model.System;
  9. using System;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Server.Implementations.Session
  13. {
  14. public class HttpSessionController : ISessionController
  15. {
  16. private readonly IHttpClient _httpClient;
  17. private readonly IJsonSerializer _json;
  18. private readonly IServerApplicationHost _appHost;
  19. public SessionInfo Session { get; private set; }
  20. //var postUrl = string.Format("http://{0}/mediabrowser/message", session.RemoteEndPoint);
  21. private readonly string _postUrl;
  22. public HttpSessionController(IHttpClient httpClient,
  23. IJsonSerializer json,
  24. IServerApplicationHost appHost,
  25. SessionInfo session,
  26. string postUrl)
  27. {
  28. _httpClient = httpClient;
  29. _json = json;
  30. _appHost = appHost;
  31. Session = session;
  32. _postUrl = postUrl;
  33. }
  34. public bool IsSessionActive
  35. {
  36. get
  37. {
  38. return (DateTime.UtcNow - Session.LastActivityDate).TotalMinutes <= 10;
  39. }
  40. }
  41. private Task SendMessage(object obj, CancellationToken cancellationToken)
  42. {
  43. var json = _json.SerializeToString(obj);
  44. return _httpClient.Post(new HttpRequestOptions
  45. {
  46. Url = _postUrl,
  47. CancellationToken = cancellationToken,
  48. RequestContent = json,
  49. RequestContentType = "application/json"
  50. });
  51. }
  52. public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  53. {
  54. return Task.FromResult(true);
  55. }
  56. public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  57. {
  58. return Task.FromResult(true);
  59. }
  60. public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  61. {
  62. return Task.FromResult(true);
  63. }
  64. public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
  65. {
  66. return SendMessage(new WebSocketMessage<PlayRequest>
  67. {
  68. MessageType = "Play",
  69. Data = command
  70. }, cancellationToken);
  71. }
  72. public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
  73. {
  74. return SendMessage(new WebSocketMessage<PlaystateRequest>
  75. {
  76. MessageType = "Playstate",
  77. Data = command
  78. }, cancellationToken);
  79. }
  80. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  81. {
  82. return Task.FromResult(true);
  83. }
  84. public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
  85. {
  86. return SendMessage(new WebSocketMessage<SystemInfo>
  87. {
  88. MessageType = "RestartRequired",
  89. Data = _appHost.GetSystemInfo()
  90. }, cancellationToken);
  91. }
  92. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  93. {
  94. return Task.FromResult(true);
  95. }
  96. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  97. {
  98. return SendMessage(new WebSocketMessage<string>
  99. {
  100. MessageType = "ServerShuttingDown",
  101. Data = string.Empty
  102. }, cancellationToken);
  103. }
  104. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  105. {
  106. return SendMessage(new WebSocketMessage<string>
  107. {
  108. MessageType = "ServerRestarting",
  109. Data = string.Empty
  110. }, cancellationToken);
  111. }
  112. public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
  113. {
  114. return SendMessage(new WebSocketMessage<GeneralCommand>
  115. {
  116. MessageType = "GeneralCommand",
  117. Data = command
  118. }, cancellationToken);
  119. }
  120. }
  121. }