diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/Session')
3 files changed, 57 insertions, 47 deletions
diff --git a/MediaBrowser.Server.Implementations/Session/HttpSessionController.cs b/MediaBrowser.Server.Implementations/Session/HttpSessionController.cs index 4d5c40853..9a7fb33df 100644 --- a/MediaBrowser.Server.Implementations/Session/HttpSessionController.cs +++ b/MediaBrowser.Server.Implementations/Session/HttpSessionController.cs @@ -43,6 +43,10 @@ namespace MediaBrowser.Server.Implementations.Session ResetPingTimer(); } + public void OnActivity() + { + } + private string PostUrl { get diff --git a/MediaBrowser.Server.Implementations/Session/SessionManager.cs b/MediaBrowser.Server.Implementations/Session/SessionManager.cs index 5ea970426..f88e21aea 100644 --- a/MediaBrowser.Server.Implementations/Session/SessionManager.cs +++ b/MediaBrowser.Server.Implementations/Session/SessionManager.cs @@ -236,34 +236,44 @@ namespace MediaBrowser.Server.Implementations.Session } var activityDate = DateTime.UtcNow; - var session = await GetSessionInfo(appName, appVersion, deviceId, deviceName, remoteEndPoint, user).ConfigureAwait(false); - + var lastActivityDate = session.LastActivityDate; session.LastActivityDate = activityDate; - if (user == null) + if (user != null) { - return session; - } - - var lastActivityDate = user.LastActivityDate; + var userLastActivityDate = user.LastActivityDate ?? DateTime.MinValue; + user.LastActivityDate = activityDate; - user.LastActivityDate = activityDate; + // Don't log in the db anymore frequently than 10 seconds + if ((activityDate - userLastActivityDate).TotalSeconds > 10) + { + try + { + // Save this directly. No need to fire off all the events for this. + await _userRepository.SaveUser(user, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.ErrorException("Error updating user", ex); + } + } + } - // Don't log in the db anymore frequently than 10 seconds - if (lastActivityDate.HasValue && (activityDate - lastActivityDate.Value).TotalSeconds < 10) + if ((activityDate - lastActivityDate).TotalSeconds > 10) { - return session; - } + EventHelper.FireEventIfNotNull(SessionActivity, this, new SessionEventArgs + { + SessionInfo = session - // Save this directly. No need to fire off all the events for this. - await _userRepository.SaveUser(user, CancellationToken.None).ConfigureAwait(false); + }, _logger); + } - EventHelper.FireEventIfNotNull(SessionActivity, this, new SessionEventArgs + var controller = session.SessionController; + if (controller != null) { - SessionInfo = session - - }, _logger); + controller.OnActivity(); + } return session; } @@ -1680,7 +1690,7 @@ namespace MediaBrowser.Server.Implementations.Session deviceId = info.DeviceId; } - return GetSessionInfo(appName, appVersion, deviceId, deviceName, remoteEndpoint, user); + return LogSessionActivity(appName, appVersion, deviceId, deviceName, remoteEndpoint, user); } public Task<SessionInfo> GetSessionByAuthenticationToken(string token, string deviceId, string remoteEndpoint) diff --git a/MediaBrowser.Server.Implementations/Session/WebSocketController.cs b/MediaBrowser.Server.Implementations/Session/WebSocketController.cs index 19aaaf8a5..765664299 100644 --- a/MediaBrowser.Server.Implementations/Session/WebSocketController.cs +++ b/MediaBrowser.Server.Implementations/Session/WebSocketController.cs @@ -30,17 +30,36 @@ namespace MediaBrowser.Server.Implementations.Session Sockets = new List<IWebSocketConnection>(); } + private bool HasOpenSockets + { + get { return GetActiveSockets().Any(); } + } + + public bool SupportsMediaControl + { + get { return HasOpenSockets; } + } + + private bool _isActive; + private DateTime _lastActivityDate; public bool IsSessionActive { get { - return Sockets.Any(i => i.State == WebSocketState.Open); + if (HasOpenSockets) + { + return true; + } + + //return false; + return _isActive && (DateTime.UtcNow - _lastActivityDate).TotalMinutes <= 10; } } - public bool SupportsMediaControl + public void OnActivity() { - get { return GetActiveSockets().Any(); } + _isActive = true; + _lastActivityDate = DateTime.UtcNow; } private IEnumerable<IWebSocketConnection> GetActiveSockets() @@ -64,6 +83,8 @@ namespace MediaBrowser.Server.Implementations.Session { if (!GetActiveSockets().Any()) { + _isActive = false; + try { _sessionManager.ReportSessionEnded(Session.Id); @@ -233,8 +254,6 @@ namespace MediaBrowser.Server.Implementations.Session private Task SendMessageInternal<T>(WebSocketMessage<T> message, CancellationToken cancellationToken) { - if (SkipSending()) return Task.FromResult(true); - var socket = GetActiveSocket(); return socket.SendAsync(message, cancellationToken); @@ -242,8 +261,6 @@ namespace MediaBrowser.Server.Implementations.Session private Task SendMessagesInternal<T>(WebSocketMessage<T> message, CancellationToken cancellationToken) { - if (SkipSending()) return Task.FromResult(true); - var tasks = GetActiveSockets().Select(i => Task.Run(async () => { try @@ -260,27 +277,6 @@ namespace MediaBrowser.Server.Implementations.Session return Task.WhenAll(tasks); } - private bool SkipSending() - { - if (Session != null) - { - if (string.Equals(Session.Client, "mb-classic", StringComparison.OrdinalIgnoreCase)) - { - Version version; - - if (!string.IsNullOrWhiteSpace(Session.ApplicationVersion) && Version.TryParse(Session.ApplicationVersion, out version)) - { - if (version < new Version(3, 0, 196)) - { - _logger.Debug("Skipping web socket message to MBC version {0}.", version); - return true; - } - } - } - } - return false; - } - public void Dispose() { foreach (var socket in Sockets.ToList()) |
