|
1 | 1 | using System; |
2 | 2 | using System.Net; |
| 3 | +using System.Net.Security; |
3 | 4 | using System.Net.WebSockets; |
4 | 5 | using System.Threading; |
5 | 6 | using System.Threading.Tasks; |
@@ -33,9 +34,11 @@ private void AllowHeaders() |
33 | 34 | .GetType() |
34 | 35 | .GetProperty("RequestHeaders", BindingFlags.NonPublic | BindingFlags.Instance); |
35 | 36 | var headers = property.GetValue(_ws.Options); |
36 | | - var hinfoField = headers.GetType().GetField("HInfo", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); |
| 37 | + var hinfoField = |
| 38 | + headers.GetType().GetField("HInfo", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); |
37 | 39 | var hinfo = hinfoField.GetValue(null); |
38 | | - var hhtField = hinfo.GetType().GetField("HeaderHashTable", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); |
| 40 | + var hhtField = |
| 41 | + hinfo.GetType().GetField("HeaderHashTable", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); |
39 | 42 | var hashTable = hhtField.GetValue(null) as System.Collections.Hashtable; |
40 | 43 |
|
41 | 44 | foreach (string key in hashTable.Keys) |
@@ -67,30 +70,43 @@ private void AllowHeaders() |
67 | 70 |
|
68 | 71 | public WebSocketState State => (WebSocketState)_ws.State; |
69 | 72 |
|
| 73 | + public RemoteCertificateValidationCallback RemoteCertificateValidationCallback { get; set; } |
| 74 | + |
70 | 75 | public async Task ConnectAsync(Uri uri, CancellationToken cancellationToken) |
71 | 76 | { |
| 77 | +#if NET6_0_OR_GREATER |
| 78 | + _ws.Options.RemoteCertificateValidationCallback = RemoteCertificateValidationCallback; |
| 79 | +#endif |
| 80 | +#if NET461_OR_GREATER |
| 81 | + ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback; |
| 82 | +#endif |
72 | 83 | await _ws.ConnectAsync(uri, cancellationToken).ConfigureAwait(false); |
73 | 84 | } |
74 | 85 |
|
75 | 86 | public async Task DisconnectAsync(CancellationToken cancellationToken) |
76 | 87 | { |
77 | | - await _ws.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, cancellationToken).ConfigureAwait(false); |
| 88 | + await _ws.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, cancellationToken) |
| 89 | + .ConfigureAwait(false); |
78 | 90 | } |
79 | 91 |
|
80 | | - public async Task SendAsync(byte[] bytes, TransportMessageType type, bool endOfMessage, CancellationToken cancellationToken) |
| 92 | + public async Task SendAsync(byte[] bytes, TransportMessageType type, bool endOfMessage, |
| 93 | + CancellationToken cancellationToken) |
81 | 94 | { |
82 | 95 | var msgType = WebSocketMessageType.Text; |
83 | 96 | if (type == TransportMessageType.Binary) |
84 | 97 | { |
85 | 98 | msgType = WebSocketMessageType.Binary; |
86 | 99 | } |
87 | | - await _ws.SendAsync(new ArraySegment<byte>(bytes), msgType, endOfMessage, cancellationToken).ConfigureAwait(false); |
| 100 | + |
| 101 | + await _ws.SendAsync(new ArraySegment<byte>(bytes), msgType, endOfMessage, cancellationToken) |
| 102 | + .ConfigureAwait(false); |
88 | 103 | } |
89 | 104 |
|
90 | 105 | public async Task<WebSocketReceiveResult> ReceiveAsync(int bufferSize, CancellationToken cancellationToken) |
91 | 106 | { |
92 | 107 | var buffer = new byte[bufferSize]; |
93 | | - var result = await _ws.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken).ConfigureAwait(false); |
| 108 | + var result = await _ws.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken) |
| 109 | + .ConfigureAwait(false); |
94 | 110 | return new WebSocketReceiveResult |
95 | 111 | { |
96 | 112 | Count = result.Count, |
|
0 commit comments