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);