Skip to content

Commit 2446f80

Browse files
authored
Merge pull request #164 from graphql-dotnet/websocket-connection-status
Websocket connection status
2 parents dfb3810 + d276793 commit 2446f80

File tree

11 files changed

+349
-220
lines changed

11 files changed

+349
-220
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace GraphQL.Client.Abstractions.Websocket {
2+
public enum GraphQLWebsocketConnectionState {
3+
Disconnected,
4+
Connecting,
5+
Connected
6+
}
7+
}

src/GraphQL.Client/GraphQLHttpClient.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Concurrent;
3-
using System.Linq;
43
using System.Net.Http;
54
using System.Text;
65
using System.Threading;
@@ -33,6 +32,11 @@ public class GraphQLHttpClient : IGraphQLClient {
3332
/// </summary>
3433
public IObservable<Exception> WebSocketReceiveErrors => graphQlHttpWebSocket.ReceiveErrors;
3534

35+
/// <summary>
36+
/// the websocket connection state
37+
/// </summary>
38+
public IObservable<GraphQLWebsocketConnectionState> WebsocketConnectionState =>
39+
graphQlHttpWebSocket.ConnectionState;
3640

3741
#region Constructors
3842

@@ -47,15 +51,15 @@ public GraphQLHttpClient(Action<GraphQLHttpClientOptions> configure) : this(conf
4751
public GraphQLHttpClient(GraphQLHttpClientOptions options, HttpClient httpClient) {
4852
Options = options;
4953
this.HttpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
50-
this.graphQlHttpWebSocket = new GraphQLHttpWebSocket(GetWebSocketUri(), Options);
54+
this.graphQlHttpWebSocket = new GraphQLHttpWebSocket(GetWebSocketUri(), this);
5155
Options.JsonSerializer = JsonSerializer.EnsureAssigned();
5256
}
5357

5458
public GraphQLHttpClient(GraphQLHttpClientOptions options, HttpClient httpClient, IGraphQLWebsocketJsonSerializer serializer) {
5559
Options = options ?? throw new ArgumentNullException(nameof(options));
5660
Options.JsonSerializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
5761
this.HttpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
58-
this.graphQlHttpWebSocket = new GraphQLHttpWebSocket(GetWebSocketUri(), Options);
62+
this.graphQlHttpWebSocket = new GraphQLHttpWebSocket(GetWebSocketUri(), this);
5963
}
6064

6165
#endregion

src/GraphQL.Client/GraphQLHttpClientOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,10 @@ public class GraphQLHttpClientOptions {
4949
/// Request preprocessing function. Can be used i.e. to inject authorization info into a GraphQL request payload.
5050
/// </summary>
5151
public Func<GraphQLRequest, GraphQLHttpClient, Task<GraphQLRequest>> PreprocessRequest { get; set; } = (request, client) => Task.FromResult(request);
52+
53+
/// <summary>
54+
/// This callback is called after successfully establishing a websocket connection but before any regular request is made.
55+
/// </summary>
56+
public Func<GraphQLHttpClient, Task> OnWebsocketConnected { get; set; } = client => Task.CompletedTask;
5257
}
5358
}

0 commit comments

Comments
 (0)