private async Task RegisterInternalAsync( ISubscription subscription, ISocketConnection connection) { if (_subs.TryAdd(subscription.Id, new Sub(subscription, connection))) { connection.Disposed += (sender, args) => RemoveSubscription(subscription.Id); if (connection.IsClosed) { _subs.TryRemove(subscription.Id, out _); return; } subscription.OnRegister(() => UnregisterAsync(subscription.Id)); var writer = new SocketMessageWriter(); writer.WriteStartObject(); writer.WriteType(MessageTypes.Subscription.Start); writer.WriteId(subscription.Id); writer.WriteStartPayload(); subscription.OperationFormatter.Serialize(subscription.Operation, writer); writer.WriteEndObject(); await connection.SendAsync(writer.Body).ConfigureAwait(false); } }
/// <summary> /// Writes a <see cref="GraphQLWebSocketMessageType.Stop"/> message to the writer /// </summary> /// <param name="writer">The writer</param> /// <param name="operationId">The operation id of the operation</param> public static void WriteStopOperationMessage( this SocketMessageWriter writer, string operationId) { if (operationId == null) { throw new ArgumentNullException(nameof(operationId)); } writer.WriteStartObject(); writer.WriteType(GraphQLWebSocketMessageType.Stop); writer.WriteId(operationId); writer.WriteEndObject(); }
public async Task UnregisterAsync(string subscriptionId) { if (_subs.TryRemove(subscriptionId, out Sub? sub)) { var writer = new SocketMessageWriter(); writer.WriteStartObject(); writer.WriteType(MessageTypes.Subscription.Stop); writer.WriteId(subscriptionId); writer.WriteEndObject(); await sub.Connection.SendAsync(writer.Body); } }
/// <summary> /// Writes a <see cref="GraphQLWebSocketMessageType.ConnectionInit"/>message to the writer /// </summary> /// <param name="writer">The writer</param> /// <param name="payload">The payload of the init message</param> public static void WriteInitializeMessage( this SocketMessageWriter writer, object?payload) { writer.WriteStartObject(); writer.WriteType(GraphQLWebSocketMessageType.ConnectionInit); if (payload is not null) { writer.WriteStartPayload(); JsonSerializer.Serialize(writer.Writer, payload); } writer.WriteEndObject(); }
/// <summary> /// Writes a <see cref="GraphQLWebSocketMessageType.Start"/> message to the writer /// </summary> /// <param name="writer">The writer</param> /// <param name="operationId">The operation id of the operation</param> /// <param name="request">The operation request containing the payload</param> public static void WriteStartOperationMessage( this SocketMessageWriter writer, string operationId, OperationRequest request) { if (operationId == null) { throw new ArgumentNullException(nameof(operationId)); } if (request == null) { throw new ArgumentNullException(nameof(request)); } writer.WriteStartObject(); writer.WriteType(GraphQLWebSocketMessageType.Start); writer.WriteId(operationId); writer.WriteStartPayload(); writer.Serialize(request); writer.WriteEndObject(); }
/// <summary> /// Writes a <see cref="GraphQLWebSocketMessageType.ConnectionTerminate"/> message to the /// writer /// </summary> /// <param name="writer">The writer</param> public static void WriteTerminateMessage(this SocketMessageWriter writer) { writer.WriteStartObject(); writer.WriteType(GraphQLWebSocketMessageType.ConnectionTerminate); writer.WriteEndObject(); }
/// <summary> /// Writes a <see cref="GraphQLWebSocketMessageType.ConnectionInit"/>message to the writer /// </summary> /// <param name="writer">The writer</param> public static void WriteInitializeMessage(this SocketMessageWriter writer) { writer.WriteStartObject(); writer.WriteType(GraphQLWebSocketMessageType.ConnectionInit); writer.WriteEndObject(); }