Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/GraphQL.Client/GraphQLClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ public async Task<GraphQLSubscriptionResult> SubscribeAsync(string query, Cancel

[Obsolete("EXPERIMENTAL API")]
public async Task<GraphQLSubscriptionResult> SubscribeAsync(GraphQLRequest request,CancellationToken cancellationToken = default) {
var a= new GraphQLSubscriptionResult();
return await Task.FromResult(a);
var graphQLSubscriptionResult = new GraphQLSubscriptionResult();
graphQLSubscriptionResult.StartAsync(cancellationToken);
return await Task.FromResult(graphQLSubscriptionResult).ConfigureAwait(false);
}

/// <summary>
Expand Down
21 changes: 17 additions & 4 deletions src/GraphQL.Client/GraphQLSubscriptionResult.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using GraphQL.Common.Response;
using Newtonsoft.Json;

namespace GraphQL.Client {

/// <summary>
/// Represents the result of a subscription query
/// </summary>
[Obsolete("EXPERIMENTAL API")]
public class GraphQLSubscriptionResult {
public class GraphQLSubscriptionResult : IDisposable {

public event Action<GraphQLResponse> OnReceive;

public GraphQLResponse LastResponse { get; }

public GraphQLSubscriptionResult() {
this.OnReceive.Invoke(null);
private readonly ClientWebSocket clientWebSocket = new ClientWebSocket();

public async void StartAsync(CancellationToken cancellationToken = default) {
await this.clientWebSocket.ConnectAsync(new Uri("ws://localhost:5000/"), cancellationToken).ConfigureAwait(false);
var arraySegment = new ArraySegment<byte>(new byte[1024]);
while (this.clientWebSocket.State == WebSocketState.Open) {
var webSocketReceiveResult = await this.clientWebSocket.ReceiveAsync(arraySegment, cancellationToken).ConfigureAwait(false);
var stringResult = Encoding.UTF8.GetString(arraySegment.Array, 0, webSocketReceiveResult.Count);
var graphQLResponse = JsonConvert.DeserializeObject<GraphQLResponse>(stringResult);
if (graphQLResponse != null) { this.OnReceive.Invoke(graphQLResponse); }
}
}

public void Dispose() => this.clientWebSocket.Dispose();

}

}