Esempio n. 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();
        }
Esempio n. 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()));
        }
Esempio n. 3
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 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
            }
        }
Esempio n. 5
0
 public void WriteSingleMessage()
 {
     ServerSentEventsMessageFormatter.WriteMessage(_rawData, Stream.Null);
 }