Skip to content

Commit

Permalink
Merge pull request #278 from graphql-dotnet/handle-websocket-close-me…
Browse files Browse the repository at this point in the history
…ssage

Handle websocket close message
  • Loading branch information
rose-a authored Sep 15, 2020
2 parents 5b59759 + 15c5e36 commit 527bf44
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/GraphQL.Client/Websocket/GraphQLHttpWebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
{
Id = startRequest.Id,
Type = GraphQLWebSocketMessageType.GQL_CONNECTION_INIT,
Payload = new GraphQLRequest()
};
var observable = Observable.Create<GraphQLResponse<TResponse>>(o =>
Expand Down Expand Up @@ -563,16 +564,23 @@ private async Task<WebsocketMessageWrapper> ReceiveWebsocketMessagesAsync()
_internalCancellationToken.ThrowIfCancellationRequested();
ms.Seek(0, SeekOrigin.Begin);

if (webSocketReceiveResult.MessageType == WebSocketMessageType.Text)
switch (webSocketReceiveResult.MessageType)
{
var response = await _client.JsonSerializer.DeserializeToWebsocketResponseWrapperAsync(ms);
response.MessageBytes = ms.ToArray();
Debug.WriteLine($"{response.MessageBytes.Length} bytes received for id {response.Id} on websocket {_clientWebSocket.GetHashCode()} (thread {Thread.CurrentThread.ManagedThreadId})...");
return response;
}
else
{
throw new NotSupportedException("binary websocket messages are not supported");
case WebSocketMessageType.Text:
var response = await _client.JsonSerializer.DeserializeToWebsocketResponseWrapperAsync(ms);
response.MessageBytes = ms.ToArray();
Debug.WriteLine($"{response.MessageBytes.Length} bytes received for id {response.Id} on websocket {_clientWebSocket.GetHashCode()} (thread {Thread.CurrentThread.ManagedThreadId})...");
return response;

case WebSocketMessageType.Close:
var closeResponse = await _client.JsonSerializer.DeserializeToWebsocketResponseWrapperAsync(ms);
closeResponse.MessageBytes = ms.ToArray();
Debug.WriteLine($"Connection closed by the server.");
throw new Exception("Connection closed by the server.");

default:
throw new NotSupportedException($"Websocket message type {webSocketReceiveResult.MessageType} not supported.");

}
}
catch (Exception e)
Expand Down Expand Up @@ -643,7 +651,7 @@ private async Task CompleteAsync()
_exceptionSubject?.OnCompleted();
_exceptionSubject?.Dispose();
_internalCancellationTokenSource.Dispose();

Debug.WriteLine("GraphQLHttpWebSocket disposed");
}

Expand Down

0 comments on commit 527bf44

Please sign in to comment.