-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathClientWebSocketFactory.cs
More file actions
65 lines (58 loc) · 2.4 KB
/
ClientWebSocketFactory.cs
File metadata and controls
65 lines (58 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using GraphQL.Client.Abstractions.Websocket;
namespace GraphQL.Client.Http.Websocket
{
#if NETSTANDARD
/// <summary>
/// Default web socket factory for netstandard2.0
/// </summary>
public class ClientWebSocketFactory : IWebSocketFactory
{
private readonly GraphQLHttpClientOptions _options;
public ClientWebSocketFactory(GraphQLHttpClientOptions options)
{
_options = options;
}
public async Task<WebSocket> ConnectAsync(Uri webSocketUri, CancellationToken cancellationToken)
{
var webSocket = new ClientWebSocket();
webSocket.Options.AddSubProtocol("graphql-ws");
// the following properties are not supported in Blazor WebAssembly and throw a PlatformNotSupportedException error when accessed
try
{
webSocket.Options.ClientCertificates = ((HttpClientHandler) _options.HttpMessageHandler).ClientCertificates;
}
catch (NotImplementedException)
{
Debug.WriteLine("property 'ClientWebSocketOptions.ClientCertificates' not implemented by current platform");
}
catch (PlatformNotSupportedException)
{
Debug.WriteLine("property 'ClientWebSocketOptions.ClientCertificates' not supported by current platform");
}
try
{
webSocket.Options.UseDefaultCredentials =
((HttpClientHandler) _options.HttpMessageHandler).UseDefaultCredentials;
}
catch (NotImplementedException)
{
Debug.WriteLine("property 'ClientWebSocketOptions.UseDefaultCredentials' not implemented by current platform");
}
catch (PlatformNotSupportedException)
{
Debug.WriteLine("Property 'ClientWebSocketOptions.UseDefaultCredentials' not supported by current platform");
}
_options.ConfigureWebsocketOptions(webSocket.Options);
Debug.WriteLine($"opening websocket {webSocket.GetHashCode()} (thread {Thread.CurrentThread.ManagedThreadId})");
await webSocket.ConnectAsync(webSocketUri, cancellationToken);
return webSocket;
}
}
#endif
}