Exemplo n.º 1
0
        private async Task RunProducer(ICitylineProducer provider, Stream stream, TicketHolder ticket, IContext context, CancellationToken cancellationToken = default)
        {
            ICitylineWriter writer;

            if (this.UseJson)
            {
                writer = new CitylineJsonWriter(semaphore, provider, stream, ticket, cancellationToken);
            }
            else
            {
                writer = new CitylineLineWriter(semaphore, provider, stream, ticket, cancellationToken);
            }

            // Func<object, Task> emitter = new Func<object, Task>(async obj =>
            // {
            //     var value = JsonConvert.SerializeObject(obj, settings);
            //     try
            //     {
            //         await semaphore.WaitAsync(cancellationToken);
            //         using (var writer = new StreamWriter(stream, new UTF8Encoding(false), value.Length + 1024, true))
            //         {
            //             if (UseJson)
            //                 await WriteJson(writer, value, provider, ticket, CancellationToken.None); // CancellationToken.None, we don't want partial messages
            //             else
            //                 await WriteText(writer, value, provider, ticket, CancellationToken.None);
            //         }
            //     }
            //     finally
            //     {
            //         semaphore.Release();
            //     }
            // });

            // context is shared, so this wont work

            var response = await provider.GetFrame(ticket, context, writer, cancellationToken);

            if (response == null)
            {
                return;
            }

            await writer.Write(response);
        }
Exemplo n.º 2
0
        private async Task RunProducer(ICitylineProducer provider, Stream stream, TicketHolder ticket, IContext context, CancellationToken cancellationToken = default)
        {
            var response = await provider.GetFrame(ticket, context, cancellationToken);

            if (response == null)
            {
                return;
            }

            lock (padLock)
            {
                using (var writer = new StreamWriter(stream, new UTF8Encoding(false), 1024, true)) {
                    writer.WriteLine($"id: {ticket.AsString()}");
                    writer.WriteLine($"event: {provider.Name}");
                    writer.WriteLine($"data: {JsonConvert.SerializeObject(response, settings)}");
                    writer.WriteLine();
                }
            }
        }