Fix websockets

pull/1010/head
Claus Vium 6 years ago
parent c0b95dbc79
commit e47d121985

@ -85,6 +85,11 @@ namespace Emby.Server.Implementations.SocketSharp
if (dispose) if (dispose)
{ {
_cancellationTokenSource.Cancel(); _cancellationTokenSource.Cancel();
if (_webSocket.State == WebSocketState.Open)
{
_webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closed by client",
CancellationToken.None);
}
} }
_disposed = true; _disposed = true;

@ -76,18 +76,23 @@ using Microsoft.Extensions.Logging;
Endpoint = endpoint Endpoint = endpoint
}); });
var buffer = WebSocket.CreateClientBuffer(4096, 4096);
WebSocketReceiveResult result; WebSocketReceiveResult result;
var message = new List<byte>(); var message = new List<byte>();
do do
{ {
var buffer = WebSocket.CreateServerBuffer(4096);
result = await webSocketContext.ReceiveAsync(buffer, _disposeCancellationToken); result = await webSocketContext.ReceiveAsync(buffer, _disposeCancellationToken);
socket.OnReceiveBytes(buffer.Array);
message.AddRange(buffer.Array.Take(result.Count)); message.AddRange(buffer.Array.Take(result.Count));
} while (!result.EndOfMessage && result.MessageType != WebSocketMessageType.Close);
if (result.EndOfMessage)
{
socket.OnReceiveBytes(message.ToArray()); socket.OnReceiveBytes(message.ToArray());
message.Clear();
}
} while (socket.State == WebSocketState.Open && result.MessageType != WebSocketMessageType.Close);
await webSocketContext.CloseAsync(result.CloseStatus ?? WebSocketCloseStatus.NormalClosure, await webSocketContext.CloseAsync(result.CloseStatus ?? WebSocketCloseStatus.NormalClosure,
result.CloseStatusDescription, _disposeCancellationToken); result.CloseStatusDescription, _disposeCancellationToken);
socket.Dispose(); socket.Dispose();

@ -80,12 +80,7 @@ namespace MediaBrowser.Controller.Net
protected readonly CultureInfo UsCulture = new CultureInfo("en-US"); protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
protected virtual bool SendOnTimer => false; protected bool SendOnTimer => false;
protected virtual void ParseMessageParams(string[] values)
{
}
/// <summary> /// <summary>
/// Starts sending messages over a web socket /// Starts sending messages over a web socket
@ -98,11 +93,6 @@ namespace MediaBrowser.Controller.Net
var dueTimeMs = long.Parse(vals[0], UsCulture); var dueTimeMs = long.Parse(vals[0], UsCulture);
var periodMs = long.Parse(vals[1], UsCulture); var periodMs = long.Parse(vals[1], UsCulture);
if (vals.Length > 2)
{
ParseMessageParams(vals.Skip(2).ToArray());
}
var cancellationTokenSource = new CancellationTokenSource(); var cancellationTokenSource = new CancellationTokenSource();
Logger.LogDebug("{1} Begin transmitting over websocket to {0}", message.Connection.RemoteEndPoint, GetType().Name); Logger.LogDebug("{1} Begin transmitting over websocket to {0}", message.Connection.RemoteEndPoint, GetType().Name);
@ -250,6 +240,7 @@ namespace MediaBrowser.Controller.Net
{ {
Logger.LogDebug("{1} stop transmitting over websocket to {0}", connection.Item1.RemoteEndPoint, GetType().Name); Logger.LogDebug("{1} stop transmitting over websocket to {0}", connection.Item1.RemoteEndPoint, GetType().Name);
connection.Item1.Dispose();
var timer = connection.Item3; var timer = connection.Item3;
if (timer != null) if (timer != null)

Loading…
Cancel
Save