Example #1
0
        public void GlobalSetup()
        {
            var        hubProtocol = new JsonHubProtocol();
            HubMessage hubMessage  = null;

            switch (Input)
            {
            case Message.NoArguments:
                hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null);
                break;

            case Message.FewArguments:
                hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, 1, "Foo", 2.0f);
                break;

            case Message.ManyArguments:
                hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, 1, "string", 2.0f, true, (byte)9, new[] { 5, 4, 3, 2, 1 }, 'c', 123456789101112L);
                break;

            case Message.LargeArguments:
                hubMessage = new InvocationMessage(target: "Target", argumentBindingException: null, new string('F', 10240), new string('B', 10240));
                break;
            }

            _parser  = new ServerSentEventsMessageParser();
            _rawData = hubProtocol.WriteToArray(hubMessage);
            var ms = new MemoryStream();

            ServerSentEventsMessageFormatter.WriteMessage(_rawData, ms);
            _sseFormattedData = ms.ToArray();
        }
Example #2
0
        public void WriteTextMessage(string encoded, string payload)
        {
            var output = new MemoryStream();

            ServerSentEventsMessageFormatter.WriteMessage(Encoding.UTF8.GetBytes(payload), output);

            Assert.Equal(encoded, Encoding.UTF8.GetString(output.ToArray()));
        }
        public async Task ProcessRequestAsync(HttpContext context, CancellationToken token)
        {
            context.Response.ContentType          = "text/event-stream";
            context.Response.Headers.CacheControl = "no-cache,no-store";
            context.Response.Headers.Pragma       = "no-cache";

            // Make sure we disable all response buffering for SSE
            var bufferingFeature = context.Features.Get <IHttpResponseBodyFeature>() !;

            bufferingFeature.DisableBuffering();

            context.Response.Headers.ContentEncoding = "identity";

            // Workaround for a Firefox bug where EventSource won't fire the open event
            // until it receives some data
            await context.Response.WriteAsync(":\r\n");

            await context.Response.Body.FlushAsync();

            try
            {
                while (true)
                {
                    var result = await _application.ReadAsync(token);

                    var buffer = result.Buffer;

                    try
                    {
                        if (result.IsCanceled)
                        {
                            break;
                        }

                        if (!buffer.IsEmpty)
                        {
                            Log.SSEWritingMessage(_logger, buffer.Length);

                            _connection?.StartSendCancellation();
                            await ServerSentEventsMessageFormatter.WriteMessageAsync(buffer, context.Response.Body, _connection?.SendingToken ?? default);
                        }
                        else if (result.IsCompleted)
                        {
                            break;
                        }
                    }
                    finally
                    {
                        _connection?.StopSendCancellation();
                        _application.AdvanceTo(buffer.End);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                // Closed connection
            }
        }
Example #4
0
        public async Task WriteTextMessageFromMultipleSegments(string encoded, string payload)
        {
            var buffer = ReadOnlySequenceFactory.SegmentPerByteFactory.CreateWithContent(Encoding.UTF8.GetBytes(payload));

            var output = new MemoryStream();
            await ServerSentEventsMessageFormatter.WriteMessageAsync(buffer, output, default);

            Assert.Equal(encoded, Encoding.UTF8.GetString(output.ToArray()));
        }
Example #5
0
        public async Task WriteTextMessageFromSingleSegment(string encoded, string payload)
        {
            var buffer = new ReadOnlySequence <byte>(Encoding.UTF8.GetBytes(payload));

            var output = new MemoryStream();
            await ServerSentEventsMessageFormatter.WriteMessageAsync(buffer, output, default);

            Assert.Equal(encoded, Encoding.UTF8.GetString(output.ToArray()));
        }
Example #6
0
        public async Task ProcessRequestAsync(HttpContext context, CancellationToken token)
        {
            context.Response.ContentType = "text/event-stream";
            context.Response.Headers["Cache-Control"] = "no-cache";

            // Make sure we disable all response buffering for SSE
            var bufferingFeature = context.Features.Get <IHttpBufferingFeature>();

            bufferingFeature?.DisableResponseBuffering();

            context.Response.Headers["Content-Encoding"] = "identity";

            // Workaround for a Firefox bug where EventSource won't fire the open event
            // until it receives some data
            await context.Response.WriteAsync(":\r\n");

            await context.Response.Body.FlushAsync();

            try
            {
                while (true)
                {
                    var result = await _application.ReadAsync(token);

                    var buffer = result.Buffer;

                    try
                    {
                        if (!buffer.IsEmpty)
                        {
                            var ms = new MemoryStream();
                            _logger.SSEWritingMessage(buffer.Length);
                            // Don't create a copy using ToArray every time
                            ServerSentEventsMessageFormatter.WriteMessage(buffer.ToArray(), ms);
                            ms.Seek(0, SeekOrigin.Begin);
                            await ms.CopyToAsync(context.Response.Body);
                        }
                        else if (result.IsCompleted)
                        {
                            break;
                        }
                    }
                    finally
                    {
                        _application.AdvanceTo(buffer.End);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                // Closed connection
            }
        }
        public void GlobalSetup()
        {
            IHubProtocol protocol;

            if (Protocol == "json")
            {
                protocol = new JsonHubProtocol();
            }
            else
            {
                // New line in result to trigger SSE formatting
                protocol = new JsonHubProtocol
                {
                    PayloadSerializer = { Formatting = Formatting.Indented }
                };
            }

            HubMessage hubMessage = null;

            switch (Input)
            {
            case Message.NoArguments:
                hubMessage = new InvocationMessage("Target", null, Array.Empty <object>());
                break;

            case Message.FewArguments:
                hubMessage = new InvocationMessage("Target", null, new object[] { 1, "Foo", 2.0f });
                break;

            case Message.ManyArguments:
                hubMessage = new InvocationMessage("Target", null, new object[] { 1, "string", 2.0f, true, (byte)9, new[] { 5, 4, 3, 2, 1 }, 'c', 123456789101112L });
                break;

            case Message.LargeArguments:
                hubMessage = new InvocationMessage("Target", null, new object[] { new string('F', 10240), new string('B', 10240) });
                break;
            }

            _parser  = new ServerSentEventsMessageParser();
            _rawData = new ReadOnlySequence <byte>(protocol.GetMessageBytes(hubMessage));
            var ms = new MemoryStream();

            ServerSentEventsMessageFormatter.WriteMessageAsync(_rawData, ms).GetAwaiter().GetResult();
            _sseFormattedData = ms.ToArray();
        }
        public async Task ProcessRequestAsync(HttpContext context, CancellationToken token)
        {
            context.Response.ContentType = "text/event-stream";
            context.Response.Headers["Cache-Control"] = "no-cache";

            // Make sure we disable all response buffering for SSE
            var bufferingFeature = context.Features.Get <IHttpBufferingFeature>();

            bufferingFeature?.DisableResponseBuffering();

            context.Response.Headers["Content-Encoding"] = "identity";

            // Workaround for a Firefox bug where EventSource won't fire the open event
            // until it receives some data
            await context.Response.WriteAsync(":\r\n");

            await context.Response.Body.FlushAsync();

            try
            {
                while (await _application.WaitToReadAsync(token))
                {
                    var ms = new MemoryStream();
                    while (_application.TryRead(out var buffer))
                    {
                        _logger.SSEWritingMessage(buffer.Length);

                        ServerSentEventsMessageFormatter.WriteMessage(buffer, ms);
                    }

                    ms.Seek(0, SeekOrigin.Begin);
                    await ms.CopyToAsync(context.Response.Body);
                }

                await _application.Completion;
            }
            catch (OperationCanceledException)
            {
                // Closed connection
            }
        }
 public Task WriteSingleMessage()
 {
     return(ServerSentEventsMessageFormatter.WriteMessageAsync(_rawData, Stream.Null));
 }
Example #10
0
 public void WriteSingleMessage()
 {
     ServerSentEventsMessageFormatter.WriteMessage(_rawData, Stream.Null);
 }