From 3a587a5d088dcb9926978a524a6e2571286df19f Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 25 May 2013 20:53:51 -0400 Subject: [PATCH] updated browse to command --- MediaBrowser.Api/SessionsService.cs | 40 ++++++++++++++----- .../Net/IWebSocketConnection.cs | 6 +++ .../ServerManager/WebSocketConnection.cs | 8 ++++ 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/MediaBrowser.Api/SessionsService.cs b/MediaBrowser.Api/SessionsService.cs index 03b90fc99..0bf0c06c6 100644 --- a/MediaBrowser.Api/SessionsService.cs +++ b/MediaBrowser.Api/SessionsService.cs @@ -51,9 +51,16 @@ namespace MediaBrowser.Api /// Artist name, genre name, item Id, etc /// /// The item identifier. - [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; } + /// + /// Gets or sets the name of the item. + /// + /// The name of the item. + [ApiMember(Name = "ItemName", Description = "The name of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] + public string ItemName { get; set; } + /// /// 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 /// /// The request. /// - 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) - { - socket.SendAsync(new WebSocketMessage - { - MessageType = "Browse", - Data = request + var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open); - }, CancellationToken.None); + if (socket != null) + { + try + { + await socket.SendAsync(new WebSocketMessage + { + MessageType = "Browse", + Data = request + + }, 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."); } } } diff --git a/MediaBrowser.Common/Net/IWebSocketConnection.cs b/MediaBrowser.Common/Net/IWebSocketConnection.cs index f0e499bd6..434b8ff5c 100644 --- a/MediaBrowser.Common/Net/IWebSocketConnection.cs +++ b/MediaBrowser.Common/Net/IWebSocketConnection.cs @@ -7,6 +7,12 @@ namespace MediaBrowser.Common.Net { public interface IWebSocketConnection : IDisposable { + /// + /// Gets the last activity date. + /// + /// The last activity date. + DateTime LastActivityDate { get; } + /// /// Gets or sets the receive action. /// diff --git a/MediaBrowser.Server.Implementations/ServerManager/WebSocketConnection.cs b/MediaBrowser.Server.Implementations/ServerManager/WebSocketConnection.cs index 6b6826bdc..0dd8cd0fd 100644 --- a/MediaBrowser.Server.Implementations/ServerManager/WebSocketConnection.cs +++ b/MediaBrowser.Server.Implementations/ServerManager/WebSocketConnection.cs @@ -50,6 +50,12 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// The receive action. public Action OnReceive { get; set; } + /// + /// Gets the last activity date. + /// + /// The last activity date. + public DateTime LastActivityDate { get; private set; } + /// /// Initializes a new instance of the class. /// @@ -90,6 +96,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// The bytes. private void OnReceiveInternal(byte[] bytes) { + LastActivityDate = DateTime.UtcNow; + if (OnReceive == null) { return;