Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        /// <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();
        }
Exemplo n.º 3
0
        /// <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();
        }