Fix sending websocket messages (#9948)
This commit is contained in:
parent
ba8f4757fd
commit
52252fcd55
|
@ -10,6 +10,7 @@ using System.Threading.Tasks;
|
||||||
using Jellyfin.Extensions.Json;
|
using Jellyfin.Extensions.Json;
|
||||||
using MediaBrowser.Controller.Net;
|
using MediaBrowser.Controller.Net;
|
||||||
using MediaBrowser.Controller.Net.WebSocketMessages;
|
using MediaBrowser.Controller.Net.WebSocketMessages;
|
||||||
|
using MediaBrowser.Controller.Net.WebSocketMessages.Outbound;
|
||||||
using MediaBrowser.Model.Session;
|
using MediaBrowser.Model.Session;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
@ -85,26 +86,15 @@ namespace Emby.Server.Implementations.HttpServer
|
||||||
/// <value>The state.</value>
|
/// <value>The state.</value>
|
||||||
public WebSocketState State => _socket.State;
|
public WebSocketState State => _socket.State;
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc />
|
||||||
/// Sends a message asynchronously.
|
public Task SendAsync(OutboundWebSocketMessage message, CancellationToken cancellationToken)
|
||||||
/// </summary>
|
|
||||||
/// <param name="message">The message.</param>
|
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
|
||||||
/// <returns>Task.</returns>
|
|
||||||
public Task SendAsync(WebSocketMessage message, CancellationToken cancellationToken)
|
|
||||||
{
|
{
|
||||||
var json = JsonSerializer.SerializeToUtf8Bytes(message, _jsonOptions);
|
var json = JsonSerializer.SerializeToUtf8Bytes(message, _jsonOptions);
|
||||||
return _socket.SendAsync(json, WebSocketMessageType.Text, true, cancellationToken);
|
return _socket.SendAsync(json, WebSocketMessageType.Text, true, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <inheritdoc />
|
||||||
/// Sends a message asynchronously.
|
public Task SendAsync<T>(OutboundWebSocketMessage<T> message, CancellationToken cancellationToken)
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The type of the message.</typeparam>
|
|
||||||
/// <param name="message">The message.</param>
|
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
|
||||||
/// <returns>Task.</returns>
|
|
||||||
public Task SendAsync<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
|
|
||||||
{
|
{
|
||||||
var json = JsonSerializer.SerializeToUtf8Bytes(message, _jsonOptions);
|
var json = JsonSerializer.SerializeToUtf8Bytes(message, _jsonOptions);
|
||||||
return _socket.SendAsync(json, WebSocketMessageType.Text, true, cancellationToken);
|
return _socket.SendAsync(json, WebSocketMessageType.Text, true, cancellationToken);
|
||||||
|
@ -183,7 +173,7 @@ namespace Emby.Server.Implementations.HttpServer
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocketMessage<object>? stub;
|
InboundWebSocketMessage<object>? stub;
|
||||||
long bytesConsumed;
|
long bytesConsumed;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -224,10 +214,10 @@ namespace Emby.Server.Implementations.HttpServer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal WebSocketMessage<object>? DeserializeWebSocketMessage(ReadOnlySequence<byte> bytes, out long bytesConsumed)
|
internal InboundWebSocketMessage<object>? DeserializeWebSocketMessage(ReadOnlySequence<byte> bytes, out long bytesConsumed)
|
||||||
{
|
{
|
||||||
var jsonReader = new Utf8JsonReader(bytes);
|
var jsonReader = new Utf8JsonReader(bytes);
|
||||||
var ret = JsonSerializer.Deserialize<WebSocketMessage<object>>(ref jsonReader, _jsonOptions);
|
var ret = JsonSerializer.Deserialize<InboundWebSocketMessage<object>>(ref jsonReader, _jsonOptions);
|
||||||
bytesConsumed = jsonReader.BytesConsumed;
|
bytesConsumed = jsonReader.BytesConsumed;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -236,11 +226,7 @@ namespace Emby.Server.Implementations.HttpServer
|
||||||
{
|
{
|
||||||
LastKeepAliveDate = DateTime.UtcNow;
|
LastKeepAliveDate = DateTime.UtcNow;
|
||||||
return SendAsync(
|
return SendAsync(
|
||||||
new OutboundWebSocketMessage
|
new OutboundKeepAliveMessage(),
|
||||||
{
|
|
||||||
MessageId = Guid.NewGuid(),
|
|
||||||
MessageType = SessionMessageType.KeepAlive
|
|
||||||
},
|
|
||||||
CancellationToken.None);
|
CancellationToken.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,9 +6,8 @@ using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Jellyfin.Api.Extensions;
|
using Jellyfin.Api.Extensions;
|
||||||
using MediaBrowser.Controller.Net;
|
using MediaBrowser.Controller.Net;
|
||||||
|
using MediaBrowser.Controller.Net.WebSocketMessages.Outbound;
|
||||||
using MediaBrowser.Controller.Session;
|
using MediaBrowser.Controller.Session;
|
||||||
using MediaBrowser.Model.Net;
|
|
||||||
using MediaBrowser.Model.Session;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
@ -308,11 +307,7 @@ namespace Emby.Server.Implementations.Session
|
||||||
private Task SendForceKeepAlive(IWebSocketConnection webSocket)
|
private Task SendForceKeepAlive(IWebSocketConnection webSocket)
|
||||||
{
|
{
|
||||||
return webSocket.SendAsync(
|
return webSocket.SendAsync(
|
||||||
new WebSocketMessage<int>
|
new ForceKeepAliveMessage(WebSocketLostTimeout),
|
||||||
{
|
|
||||||
MessageType = SessionMessageType.ForceKeepAlive,
|
|
||||||
Data = WebSocketLostTimeout
|
|
||||||
},
|
|
||||||
CancellationToken.None);
|
CancellationToken.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -171,7 +171,6 @@ namespace MediaBrowser.Controller.Net
|
||||||
await connection.SendAsync(
|
await connection.SendAsync(
|
||||||
new OutboundWebSocketMessage<TReturnDataType>
|
new OutboundWebSocketMessage<TReturnDataType>
|
||||||
{
|
{
|
||||||
MessageId = Guid.NewGuid(),
|
|
||||||
MessageType = Type,
|
MessageType = Type,
|
||||||
Data = data
|
Data = data
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,6 +5,7 @@ using System.Net;
|
||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using MediaBrowser.Controller.Net.WebSocketMessages;
|
||||||
|
|
||||||
namespace MediaBrowser.Controller.Net
|
namespace MediaBrowser.Controller.Net
|
||||||
{
|
{
|
||||||
|
@ -45,6 +46,15 @@ namespace MediaBrowser.Controller.Net
|
||||||
/// <value>The remote end point.</value>
|
/// <value>The remote end point.</value>
|
||||||
IPAddress? RemoteEndPoint { get; }
|
IPAddress? RemoteEndPoint { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sends a message asynchronously.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">The message.</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
|
/// <returns>Task.</returns>
|
||||||
|
/// <exception cref="ArgumentNullException">The message is null.</exception>
|
||||||
|
Task SendAsync(OutboundWebSocketMessage message, CancellationToken cancellationToken);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends a message asynchronously.
|
/// Sends a message asynchronously.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -53,7 +63,7 @@ namespace MediaBrowser.Controller.Net
|
||||||
/// <param name="cancellationToken">The cancellation token.</param>
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
/// <returns>Task.</returns>
|
/// <returns>Task.</returns>
|
||||||
/// <exception cref="ArgumentNullException">The message is null.</exception>
|
/// <exception cref="ArgumentNullException">The message is null.</exception>
|
||||||
Task SendAsync<T>(WebSocketMessage<T> message, CancellationToken cancellationToken);
|
Task SendAsync<T>(OutboundWebSocketMessage<T> message, CancellationToken cancellationToken);
|
||||||
|
|
||||||
Task ProcessAsync(CancellationToken cancellationToken = default);
|
Task ProcessAsync(CancellationToken cancellationToken = default);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
#nullable disable
|
#nullable disable
|
||||||
|
|
||||||
|
using MediaBrowser.Controller.Net.WebSocketMessages;
|
||||||
|
|
||||||
namespace MediaBrowser.Controller.Net
|
namespace MediaBrowser.Controller.Net
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class WebSocketMessageInfo.
|
/// Class WebSocketMessageInfo.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class WebSocketMessageInfo : WebSocketMessage<string>
|
public class WebSocketMessageInfo : InboundWebSocketMessage<string>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the connection.
|
/// Gets or sets the connection.
|
||||||
|
|
|
@ -6,13 +6,12 @@ namespace MediaBrowser.Controller.Net;
|
||||||
/// Class WebSocketMessage.
|
/// Class WebSocketMessage.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The type of the data.</typeparam>
|
/// <typeparam name="T">The type of the data.</typeparam>
|
||||||
// TODO make this abstract, remove empty ctor.
|
public abstract class WebSocketMessage<T> : WebSocketMessage
|
||||||
public class WebSocketMessage<T> : WebSocketMessage
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="WebSocketMessage{T}"/> class.
|
/// Initializes a new instance of the <see cref="WebSocketMessage{T}"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public WebSocketMessage()
|
protected WebSocketMessage()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,5 +10,5 @@ public class OutboundWebSocketMessage : WebSocketMessage, IOutboundWebSocketMess
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the message id.
|
/// Gets or sets the message id.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Guid MessageId { get; set; }
|
public Guid MessageId { get; set; } = Guid.NewGuid();
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,5 +29,5 @@ public class OutboundWebSocketMessage<T> : WebSocketMessage<T>, IOutboundWebSock
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the message id.
|
/// Gets or sets the message id.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Guid MessageId { get; set; }
|
public Guid MessageId { get; set; } = Guid.NewGuid();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user