2013-09-24 15:54:42 -04:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
2013-09-24 11:42:30 -04:00
|
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
|
using MediaBrowser.Model.Session;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
{
|
2013-10-02 15:08:58 -04:00
|
|
|
|
public class WebSocketController : ISessionController
|
2013-09-24 11:42:30 -04:00
|
|
|
|
{
|
|
|
|
|
public bool Supports(SessionInfo session)
|
|
|
|
|
{
|
|
|
|
|
return session.WebSockets.Any(i => i.State == WebSocketState.Open);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 15:54:42 -04:00
|
|
|
|
private IWebSocketConnection GetSocket(SessionInfo session)
|
2013-09-24 11:42:30 -04:00
|
|
|
|
{
|
|
|
|
|
var socket = session.WebSockets.OrderByDescending(i => i.LastActivityDate).FirstOrDefault(i => i.State == WebSocketState.Open);
|
|
|
|
|
|
2013-09-24 15:54:42 -04:00
|
|
|
|
|
|
|
|
|
if (socket == null)
|
2013-09-24 11:42:30 -04:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("The requested session does not have an open web socket.");
|
|
|
|
|
}
|
2013-09-24 15:54:42 -04:00
|
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendSystemCommand(SessionInfo session, SystemCommand command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetSocket(session);
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<string>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "SystemCommand",
|
|
|
|
|
Data = command.ToString()
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendMessageCommand(SessionInfo session, MessageCommand command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetSocket(session);
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<MessageCommand>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "MessageCommand",
|
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendPlayCommand(SessionInfo session, PlayRequest command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetSocket(session);
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<PlayRequest>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "Play",
|
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendBrowseCommand(SessionInfo session, BrowseRequest command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetSocket(session);
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<BrowseRequest>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "Browse",
|
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendPlaystateCommand(SessionInfo session, PlaystateRequest command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetSocket(session);
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<PlaystateRequest>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "Playstate",
|
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
2013-09-24 11:42:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|