Пример #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);
            }
        }
Пример #2
0
        /// <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();
        }
Пример #3
0
        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);
            }
        }
Пример #4
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();
        }
Пример #5
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();
        }
Пример #6
0
 /// <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();
 }
Пример #7
0
 /// <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();
 }