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 } }
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())); }
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())); }
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 Task WriteSingleMessage() { return(ServerSentEventsMessageFormatter.WriteMessageAsync(_rawData, Stream.Null)); }