mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-03-06 10:16:18 +00:00
reduce byte conversions with alchemy web socket
This commit is contained in:
@@ -90,6 +90,10 @@ namespace MediaBrowser.Server.Implementations.WebSocket
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
if (WebSocketServer != null)
|
||||
{
|
||||
WebSocketServer.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -107,7 +111,10 @@ namespace MediaBrowser.Server.Implementations.WebSocket
|
||||
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
||||
protected virtual void Dispose(bool dispose)
|
||||
{
|
||||
|
||||
if (WebSocketServer != null)
|
||||
{
|
||||
WebSocketServer.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Net;
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -42,7 +41,7 @@ namespace MediaBrowser.Server.Implementations.WebSocket
|
||||
UserContext = context;
|
||||
|
||||
context.SetOnDisconnect(OnDisconnected);
|
||||
context.SetOnReceive(OnReceive);
|
||||
context.SetOnReceive(OnReceiveContext);
|
||||
|
||||
_logger.Info("Client connected from {0}", context.ClientAddress);
|
||||
}
|
||||
@@ -50,7 +49,7 @@ namespace MediaBrowser.Server.Implementations.WebSocket
|
||||
/// <summary>
|
||||
/// The _disconnected
|
||||
/// </summary>
|
||||
private bool _disconnected = false;
|
||||
private bool _disconnected;
|
||||
/// <summary>
|
||||
/// Gets or sets the state.
|
||||
/// </summary>
|
||||
@@ -73,25 +72,13 @@ namespace MediaBrowser.Server.Implementations.WebSocket
|
||||
/// Called when [receive].
|
||||
/// </summary>
|
||||
/// <param name="context">The context.</param>
|
||||
private void OnReceive(UserContext context)
|
||||
private void OnReceiveContext(UserContext context)
|
||||
{
|
||||
if (OnReceiveDelegate != null)
|
||||
if (OnReceive != null)
|
||||
{
|
||||
var json = context.DataFrame.ToString();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
try
|
||||
{
|
||||
var bytes = Encoding.UTF8.GetBytes(json);
|
||||
|
||||
OnReceiveDelegate(bytes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error processing web socket message", ex);
|
||||
}
|
||||
}
|
||||
OnReceive(json);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +115,12 @@ namespace MediaBrowser.Server.Implementations.WebSocket
|
||||
/// Gets or sets the receive action.
|
||||
/// </summary>
|
||||
/// <value>The receive action.</value>
|
||||
public Action<byte[]> OnReceiveDelegate { get; set; }
|
||||
public Action<byte[]> OnReceiveBytes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the on receive.
|
||||
/// </summary>
|
||||
/// <value>The on receive.</value>
|
||||
public Action<string> OnReceive { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
54
MediaBrowser.Server.Implementations/WebSocket/FleckServer.cs
Normal file
54
MediaBrowser.Server.Implementations/WebSocket/FleckServer.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Fleck;
|
||||
using MediaBrowser.Common.Net;
|
||||
using System;
|
||||
using IWebSocketServer = MediaBrowser.Common.Net.IWebSocketServer;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.WebSocket
|
||||
{
|
||||
public class FleckServer : IWebSocketServer
|
||||
{
|
||||
private WebSocketServer _server;
|
||||
|
||||
public void Start(int portNumber)
|
||||
{
|
||||
var server = new WebSocketServer("ws://localhost:" + portNumber);
|
||||
|
||||
server.Start(socket =>
|
||||
{
|
||||
socket.OnOpen = () => OnClientConnected(socket);
|
||||
});
|
||||
|
||||
_server = server;
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_server.Dispose();
|
||||
}
|
||||
|
||||
private void OnClientConnected(Fleck.IWebSocketConnection context)
|
||||
{
|
||||
if (WebSocketConnected != null)
|
||||
{
|
||||
var socket = new FleckWebSocket(context);
|
||||
|
||||
WebSocketConnected(this, new WebSocketConnectEventArgs
|
||||
{
|
||||
WebSocket = socket,
|
||||
Endpoint = context.ConnectionInfo.ClientIpAddress + ":" + context.ConnectionInfo.ClientPort
|
||||
});
|
||||
}
|
||||
}
|
||||
public event EventHandler<WebSocketConnectEventArgs> WebSocketConnected;
|
||||
|
||||
public int Port
|
||||
{
|
||||
get { return _server.Port; }
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_server.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Model.Net;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using IWebSocketConnection = Fleck.IWebSocketConnection;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.WebSocket
|
||||
{
|
||||
public class FleckWebSocket : IWebSocket
|
||||
{
|
||||
private readonly IWebSocketConnection _connection;
|
||||
|
||||
public FleckWebSocket(IWebSocketConnection connection)
|
||||
{
|
||||
_connection = connection;
|
||||
|
||||
_connection.OnMessage = OnReceiveData;
|
||||
}
|
||||
|
||||
public WebSocketState State
|
||||
{
|
||||
get { return _connection.IsAvailable ? WebSocketState.Open : WebSocketState.Closed; }
|
||||
}
|
||||
|
||||
private void OnReceiveData(string data)
|
||||
{
|
||||
if (OnReceive != null)
|
||||
{
|
||||
OnReceive(data);
|
||||
}
|
||||
}
|
||||
|
||||
public Task SendAsync(byte[] bytes, WebSocketMessageType type, bool endOfMessage, CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.Run(() => _connection.Send(bytes));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_connection.Close();
|
||||
}
|
||||
|
||||
public Action<byte[]> OnReceiveBytes { get; set; }
|
||||
public Action<string> OnReceive { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user