Fix broken SSE transport

This commit is contained in:
David Fowler 2017-06-23 14:27:03 -07:00
parent ef273b4796
commit 9602787463
2 changed files with 29 additions and 4 deletions

View File

@ -44,9 +44,9 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Transports
try
{
var ms = new MemoryStream();
while (await _application.WaitToReadAsync(token))
{
var ms = new MemoryStream();
while (_application.TryRead(out var buffer))
{
_logger.SSEWritingMessage(_connectionId, buffer.Length);
@ -61,10 +61,10 @@ namespace Microsoft.AspNetCore.Sockets.Internal.Transports
throw new InvalidOperationException("Ran out of space to format messages!");
}
}
}
ms.Seek(0, SeekOrigin.Begin);
await ms.CopyToAsync(context.Response.Body);
ms.Seek(0, SeekOrigin.Begin);
await ms.CopyToAsync(context.Response.Body);
}
await _application.Completion;
}

View File

@ -7,6 +7,7 @@ using System.Threading.Tasks;
using System.Threading.Tasks.Channels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.SignalR.Tests.Common;
using Microsoft.AspNetCore.Sockets.Internal.Transports;
using Microsoft.Extensions.Logging;
using Xunit;
@ -46,6 +47,30 @@ namespace Microsoft.AspNetCore.Sockets.Tests
Assert.True(feature.ResponseBufferingDisabled);
}
[Fact]
public async Task SSEWritesMessages()
{
var channel = Channel.CreateUnbounded<byte[]>(new ChannelOptimizations
{
AllowSynchronousContinuations = true
});
var context = new DefaultHttpContext();
var ms = new MemoryStream();
context.Response.Body = ms;
var sse = new ServerSentEventsTransport(channel, connectionId: string.Empty, loggerFactory: new LoggerFactory());
var task = sse.ProcessRequestAsync(context, context.RequestAborted);
await channel.Out.WriteAsync(Encoding.ASCII.GetBytes("Hello"));
Assert.Equal(":\r\ndata: Hello\r\n\r\n", Encoding.ASCII.GetString(ms.ToArray()));
channel.Out.TryComplete();
await task.OrTimeout();
}
[Theory]
[InlineData("Hello World", ":\r\ndata: Hello World\r\n\r\n")]
[InlineData("Hello\nWorld", ":\r\ndata: Hello\r\ndata: World\r\n\r\n")]