using Alchemy;
using Alchemy.Classes;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Logging;
using System;
using System.Net;
namespace MediaBrowser.Common.Implementations.WebSocket
{
///
/// Class AlchemyServer
///
public class AlchemyServer : IWebSocketServer
{
///
/// Occurs when [web socket connected].
///
public event EventHandler WebSocketConnected;
///
/// Gets or sets the web socket server.
///
/// The web socket server.
private WebSocketServer WebSocketServer { get; set; }
///
/// The _logger
///
private readonly ILogger _logger;
///
/// Initializes a new instance of the class.
///
/// The logger.
/// logger
public AlchemyServer(ILogger logger)
{
if (logger == null)
{
throw new ArgumentNullException("logger");
}
_logger = logger;
}
///
/// Gets the port.
///
/// The port.
public int Port { get; private set; }
///
/// Starts the specified port number.
///
/// The port number.
public void Start(int portNumber)
{
WebSocketServer = new WebSocketServer(portNumber, IPAddress.Any)
{
OnConnected = OnAlchemyWebSocketClientConnected,
TimeOut = TimeSpan.FromMinutes(60)
};
WebSocketServer.Start();
Port = portNumber;
_logger.Info("Alchemy Web Socket Server started");
}
///
/// Called when [alchemy web socket client connected].
///
/// The context.
private void OnAlchemyWebSocketClientConnected(UserContext context)
{
if (WebSocketConnected != null)
{
var socket = new AlchemyWebSocket(context, _logger);
WebSocketConnected(this, new WebSocketConnectEventArgs
{
WebSocket = socket,
Endpoint = context.ClientAddress.ToString()
});
}
}
///
/// Stops this instance.
///
public void Stop()
{
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases unmanaged and - optionally - managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected virtual void Dispose(bool dispose)
{
}
}
}