Still broken

This commit is contained in:
Claus Vium 2019-02-26 15:13:06 +01:00
parent 38f52a139e
commit d6c6f3c10c
5 changed files with 49 additions and 33 deletions

View File

@ -740,7 +740,7 @@ namespace Emby.Server.Implementations
var request = context.Request;
var response = context.Response;
var localPath = context.Request.Path.ToString().TrimStart('/');
var localPath = context.Request.Path.ToString();
var req = new WebSocketSharpRequest(request, response, request.Path, Logger);
await ((HttpListenerHost)HttpServer).RequestHandler(req, request.GetDisplayUrl(), request.Host.ToString(), localPath, CancellationToken.None).ConfigureAwait(false);

View File

@ -1,4 +1,5 @@
using System;
using System.Net.WebSockets;
using MediaBrowser.Model.Services;
namespace Emby.Server.Implementations.Net

View File

@ -1,5 +1,6 @@
using System;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Emby.Server.Implementations.Net;
@ -20,25 +21,18 @@ namespace Emby.Server.Implementations.SocketSharp
/// Gets or sets the web socket.
/// </summary>
/// <value>The web socket.</value>
private SocketHttpListener.WebSocket WebSocket { get; set; }
private WebSocket WebSocket { get; set; }
private TaskCompletionSource<bool> _taskCompletionSource = new TaskCompletionSource<bool>();
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private bool _disposed = false;
public SharpWebSocket(SocketHttpListener.WebSocket socket, ILogger logger)
public SharpWebSocket(WebSocket socket, ILogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
WebSocket = socket ?? throw new ArgumentNullException(nameof(socket));
socket.OnMessage += OnSocketMessage;
socket.OnClose += OnSocketClose;
socket.OnError += OnSocketError;
}
public Task ConnectAsServerAsync()
=> WebSocket.ConnectAsServer();
public Task StartReceive()
{
return _taskCompletionSource.Task;
@ -58,7 +52,7 @@ namespace Emby.Server.Implementations.SocketSharp
Closed?.Invoke(this, EventArgs.Empty);
}
private void OnSocketMessage(object sender, SocketHttpListener.MessageEventArgs e)
private void OnSocketMessage(SocketHttpListener.MessageEventArgs e)
{
if (OnReceiveBytes != null)
{
@ -66,11 +60,15 @@ namespace Emby.Server.Implementations.SocketSharp
}
}
public Task ConnectAsServerAsync()
{
return Task.CompletedTask;
}
/// <summary>
/// Gets or sets the state.
/// </summary>
/// <value>The state.</value>
public WebSocketState State => WebSocket.ReadyState;
public WebSocketState State => WebSocket.State;
/// <summary>
/// Sends the async.
@ -81,7 +79,7 @@ namespace Emby.Server.Implementations.SocketSharp
/// <returns>Task.</returns>
public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken)
{
return WebSocket.SendAsync(bytes);
return WebSocket.SendAsync(new ArraySegment<byte>(bytes), WebSocketMessageType.Binary, endOfMessage, cancellationToken);
}
/// <summary>
@ -93,7 +91,7 @@ namespace Emby.Server.Implementations.SocketSharp
/// <returns>Task.</returns>
public Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken)
{
return WebSocket.SendAsync(text);
return WebSocket.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(text)), WebSocketMessageType.Text, endOfMessage, cancellationToken);
}
/// <summary>
@ -118,13 +116,10 @@ namespace Emby.Server.Implementations.SocketSharp
if (dispose)
{
WebSocket.OnMessage -= OnSocketMessage;
WebSocket.OnClose -= OnSocketClose;
WebSocket.OnError -= OnSocketError;
_cancellationTokenSource.Cancel();
WebSocket.CloseAsync().GetAwaiter().GetResult();
// TODO
WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "bye", CancellationToken.None).GetAwaiter().GetResult();
}
_disposed = true;

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using Emby.Server.Implementations.HttpServer;
@ -144,22 +145,41 @@ using Microsoft.Extensions.Logging;
_logger.LogDebug("Web socket connection allowed");
var webSocketContext = await ctx.WebSockets.AcceptWebSocketAsync(null).ConfigureAwait(false);
var socket = new SharpWebSocket(webSocketContext, _logger);
await socket.ConnectAsServerAsync().ConfigureAwait(false);
if (WebSocketConnected != null)
WebSocketConnected(new WebSocketConnectEventArgs
{
//SharpWebSocket socket = new SharpWebSocket(webSocketContext, _logger);
//await socket.ConnectAsServerAsync().ConfigureAwait(false);
Url = url,
QueryString = queryString,
WebSocket = socket,
Endpoint = endpoint
});
//WebSocketConnected(new WebSocketConnectEventArgs
//{
// Url = url,
// QueryString = queryString,
// WebSocket = socket,
// Endpoint = endpoint
//});
//await ReceiveWebSocketAsync(ctx, socket).ConfigureAwait(false);
var buffer = WebSocket.CreateClientBuffer(1024 * 4, 1024 * 4);
WebSocketReceiveResult result = await webSocketContext.ReceiveAsync(buffer, CancellationToken.None);
socket.OnReceiveBytes(buffer.Array);
//while (!result.CloseStatus.HasValue)
//{
// await webSocketContext.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
//await ReceiveWebSocketAsync(ctx, socket).ConfigureAwait(false);
}
// result = await webSocketContext.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
//}
//WebSocketConnected?.Invoke(new WebSocketConnectEventArgs
//{
// Url = url,
// QueryString = queryString,
// WebSocket = webSocketContext,
// Endpoint = endpoint
//});
await webSocketContext.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
//SharpWebSocket socket = new SharpWebSocket(webSocketContext, _logger);
//await socket.ConnectAsServerAsync().ConfigureAwait(false);
//await ReceiveWebSocketAsync(ctx, socket).ConfigureAwait(false);
}
else
{

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
@ -152,7 +152,7 @@ using Microsoft.Extensions.Logging;
{
Url = url,
QueryString = queryString,
WebSocket = socket,
WebSocket = null, // socket,
Endpoint = endpoint
});