Bläddra i källkod

updated browse to command

Luke Pulverenti 12 år sedan
förälder
incheckning
3a587a5d08

+ 28 - 8
MediaBrowser.Api/SessionsService.cs

@@ -51,9 +51,16 @@ namespace MediaBrowser.Api
         /// Artist name, genre name, item Id, etc
         /// </summary>
         /// <value>The item identifier.</value>
-        [ApiMember(Name = "ItemIdentifier", Description = "The Id of the item, unless it is an Artist, Genre, Studio, or Person, in which case it should be the name.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
-        public string ItemIdentifier { get; set; }
+        [ApiMember(Name = "ItemId", Description = "The Id of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
+        public string ItemId { get; set; }
 
+        /// <summary>
+        /// Gets or sets the name of the item.
+        /// </summary>
+        /// <value>The name of the item.</value>
+        [ApiMember(Name = "ItemName", Description = "The name of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
+        public string ItemName { get; set; }
+        
         /// <summary>
         /// Gets or sets the context (Movies, Music, TvShows, etc)
         /// Applicable to genres, studios and persons only because the context of items and artists can be inferred.
@@ -105,7 +112,7 @@ namespace MediaBrowser.Api
         /// </summary>
         /// <param name="request">The request.</param>
         /// <exception cref="ResourceNotFoundException"></exception>
-        public void Post(BrowseTo request)
+        public async void Post(BrowseTo request)
         {
             var session = _sessionManager.Sessions.FirstOrDefault(i => i.Id == request.Id);
 
@@ -114,14 +121,27 @@ namespace MediaBrowser.Api
                 throw new ResourceNotFoundException(string.Format("Session {0} not found.", request.Id));
             }
 
-            foreach (var socket in session.WebSockets)
+            var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open);
+
+            if (socket != null)
             {
-                socket.SendAsync(new WebSocketMessage<BrowseTo>
+                try
                 {
-                    MessageType = "Browse",
-                    Data = request
+                    await socket.SendAsync(new WebSocketMessage<BrowseTo>
+                    {
+                        MessageType = "Browse",
+                        Data = request
 
-                }, CancellationToken.None);
+                    }, CancellationToken.None).ConfigureAwait(false);
+                }
+                catch (Exception ex)
+                {
+                    Logger.ErrorException("Error sending web socket message", ex);
+                }
+            }
+            else
+            {
+                throw new InvalidOperationException("The requested session does not have an open web socket.");
             }
         }
     }

+ 6 - 0
MediaBrowser.Common/Net/IWebSocketConnection.cs

@@ -7,6 +7,12 @@ namespace MediaBrowser.Common.Net
 {
     public interface IWebSocketConnection : IDisposable
     {
+        /// <summary>
+        /// Gets the last activity date.
+        /// </summary>
+        /// <value>The last activity date.</value>
+        DateTime LastActivityDate { get; }
+
         /// <summary>
         /// Gets or sets the receive action.
         /// </summary>

+ 8 - 0
MediaBrowser.Server.Implementations/ServerManager/WebSocketConnection.cs

@@ -50,6 +50,12 @@ namespace MediaBrowser.Server.Implementations.ServerManager
         /// <value>The receive action.</value>
         public Action<WebSocketMessageInfo> OnReceive { get; set; }
 
+        /// <summary>
+        /// Gets the last activity date.
+        /// </summary>
+        /// <value>The last activity date.</value>
+        public DateTime LastActivityDate { get; private set; }
+
         /// <summary>
         /// Initializes a new instance of the <see cref="WebSocketConnection" /> class.
         /// </summary>
@@ -90,6 +96,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager
         /// <param name="bytes">The bytes.</param>
         private void OnReceiveInternal(byte[] bytes)
         {
+            LastActivityDate = DateTime.UtcNow;
+
             if (OnReceive == null)
             {
                 return;