From 54779bb793be37e12c87238dda8eba89c530e484 Mon Sep 17 00:00:00 2001 From: Dmitri Shimanski Date: Sun, 30 Mar 2025 11:33:41 +0300 Subject: [PATCH] Adds jsonSerializer for sendMessage for better user experience --- yawaflua.WebSockets/Core/WebSocket.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/yawaflua.WebSockets/Core/WebSocket.cs b/yawaflua.WebSockets/Core/WebSocket.cs index 99c52b1..a875020 100644 --- a/yawaflua.WebSockets/Core/WebSocket.cs +++ b/yawaflua.WebSockets/Core/WebSocket.cs @@ -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 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);