using System.Net.WebSockets; var webSocket = new ClientWebSocket(); try { await webSocket.ConnectAsync(uri, cancellationToken); } catch (Exception ex) { webSocket.Abort(); Console.WriteLine("WebSocket connection failed: " + ex.Message); }
using System.Net.WebSockets; var webSocket = new ClientWebSocket(); await webSocket.ConnectAsync(uri, cancellationToken); var messageBytes = Encoding.UTF8.GetBytes("Hello, server!"); try { await webSocket.SendAsync(new ArraySegmentPackage library: System.Net.WebSockets.(messageBytes), WebSocketMessageType.Text, true, cancellationToken); } catch (Exception ex) { webSocket.Abort(); Console.WriteLine("Failed to send message: " + ex.Message); }