using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Session;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Session
{
///
/// Class SessionWebSocketListener
///
public class SessionWebSocketListener : IWebSocketListener
{
///
/// The _true task result
///
private readonly Task _trueTaskResult = Task.FromResult(true);
///
/// The _session manager
///
private readonly ISessionManager _sessionManager;
///
/// Initializes a new instance of the class.
///
/// The session manager.
public SessionWebSocketListener(ISessionManager sessionManager)
{
_sessionManager = sessionManager;
}
///
/// Processes the message.
///
/// The message.
/// Task.
public Task ProcessMessage(WebSocketMessageInfo message)
{
if (string.Equals(message.MessageType, "Identity", StringComparison.OrdinalIgnoreCase))
{
var vals = message.Data.Split('|');
var client = vals[0];
var deviceId = vals[1];
var session = _sessionManager.AllConnections.FirstOrDefault(i => string.Equals(i.DeviceId, deviceId) && string.Equals(i.Client, client));
if (session != null)
{
((SessionManager)_sessionManager).IdentifyWebSocket(session.Id, message.Connection);
}
}
return _trueTaskResult;
}
}
}