Adds jsonSerializer for sendMessage for better user experience

This commit is contained in:
Dmitri Shimanski
2025-03-30 11:33:41 +03:00
parent dc762db048
commit 54779bb793

View File

@@ -1,5 +1,6 @@
using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
using yawaflua.WebSockets.Models.Interfaces;
namespace yawaflua.WebSockets.Core;
@@ -37,6 +38,13 @@ internal class WebSocket : IWebSocket
messageType,
true,
cts);
public async Task SendAsync<T>(T message, WebSocketMessageType messageType = WebSocketMessageType.Text, CancellationToken cts = default)
=> await _webSocket.SendAsync(
Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message)),
messageType,
true,
cts);
public async Task CloseAsync(WebSocketCloseStatus closeStatus = WebSocketCloseStatus.NormalClosure, string? reason = null, CancellationToken cts = default)
=> await _webSocket.CloseAsync(closeStatus, reason, cts);