|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
|
using MediaBrowser.Model.Session;
|
|
|
|
|
using MediaBrowser.Model.System;
|
|
|
|
@ -19,11 +20,13 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
public List<IWebSocketConnection> Sockets { get; private set; }
|
|
|
|
|
|
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
public WebSocketController(SessionInfo session, IServerApplicationHost appHost)
|
|
|
|
|
public WebSocketController(SessionInfo session, IServerApplicationHost appHost, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
Session = session;
|
|
|
|
|
_appHost = appHost;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
Sockets = new List<IWebSocketConnection>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -35,11 +38,17 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IWebSocketConnection GetActiveSocket()
|
|
|
|
|
private IEnumerable<IWebSocketConnection> GetActiveSockets()
|
|
|
|
|
{
|
|
|
|
|
var socket = Sockets
|
|
|
|
|
return Sockets
|
|
|
|
|
.OrderByDescending(i => i.LastActivityDate)
|
|
|
|
|
.FirstOrDefault(i => i.State == WebSocketState.Open);
|
|
|
|
|
.Where(i => i.State == WebSocketState.Open);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IWebSocketConnection GetActiveSocket()
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSockets()
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (socket == null)
|
|
|
|
|
{
|
|
|
|
@ -51,9 +60,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
|
|
|
|
|
public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<PlayRequest>
|
|
|
|
|
return SendMessage(new WebSocketMessage<PlayRequest>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "Play",
|
|
|
|
|
Data = command
|
|
|
|
@ -63,9 +70,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
|
|
|
|
|
public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<PlaystateRequest>
|
|
|
|
|
return SendMessage(new WebSocketMessage<PlaystateRequest>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "Playstate",
|
|
|
|
|
Data = command
|
|
|
|
@ -75,9 +80,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
|
|
|
|
|
public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<LibraryUpdateInfo>
|
|
|
|
|
return SendMessages(new WebSocketMessage<LibraryUpdateInfo>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "LibraryChanged",
|
|
|
|
|
Data = info
|
|
|
|
@ -92,9 +95,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<SystemInfo>
|
|
|
|
|
return SendMessages(new WebSocketMessage<SystemInfo>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "RestartRequired",
|
|
|
|
|
Data = _appHost.GetSystemInfo()
|
|
|
|
@ -111,9 +112,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<UserDataChangeInfo>
|
|
|
|
|
return SendMessages(new WebSocketMessage<UserDataChangeInfo>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "UserDataChanged",
|
|
|
|
|
Data = info
|
|
|
|
@ -128,9 +127,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendServerShutdownNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<string>
|
|
|
|
|
return SendMessages(new WebSocketMessage<string>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "ServerShuttingDown",
|
|
|
|
|
Data = string.Empty
|
|
|
|
@ -145,9 +142,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendServerRestartNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<string>
|
|
|
|
|
return SendMessages(new WebSocketMessage<string>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "ServerRestarting",
|
|
|
|
|
Data = string.Empty
|
|
|
|
@ -157,9 +152,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
|
|
|
|
|
public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<GeneralCommand>
|
|
|
|
|
return SendMessage(new WebSocketMessage<GeneralCommand>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "GeneralCommand",
|
|
|
|
|
Data = command
|
|
|
|
@ -169,9 +162,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
|
|
|
|
|
public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<SessionInfoDto>
|
|
|
|
|
return SendMessages(new WebSocketMessage<SessionInfoDto>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "SessionEnded",
|
|
|
|
|
Data = sessionInfo
|
|
|
|
@ -181,9 +172,7 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
|
|
|
|
|
public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<SessionInfoDto>
|
|
|
|
|
return SendMessages(new WebSocketMessage<SessionInfoDto>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "PlaybackStart",
|
|
|
|
|
Data = sessionInfo
|
|
|
|
@ -193,14 +182,37 @@ namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
|
|
|
|
|
public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<SessionInfoDto>
|
|
|
|
|
return SendMessages(new WebSocketMessage<SessionInfoDto>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "PlaybackStopped",
|
|
|
|
|
Data = sessionInfo
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task SendMessage<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(message, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task SendMessages<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var tasks = GetActiveSockets().Select(i => Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await i.SendAsync(message, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error sending web socket message", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, cancellationToken));
|
|
|
|
|
|
|
|
|
|
return Task.WhenAll(tasks);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|