Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions EvtSource/EventSourceReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace EvtSource
public class EventSourceReader : IDisposable
{
private const string DefaultEventType = "message";
private const string EventStreamMimeType = "text/event-stream";

public delegate void MessageReceivedHandler(object sender, EventSourceMessageEventArgs e);
public delegate void DisconnectEventHandler(object sender, DisconnectEventArgs e);
Expand Down Expand Up @@ -80,7 +81,6 @@ public void Dispose()
Hc.Dispose();
}


private async Task ReaderAsync()
{
try
Expand All @@ -92,12 +92,13 @@ private async Task ReaderAsync()
Hc.DefaultRequestHeaders.Remove("Last-Event-Id");
}

Hc.DefaultRequestHeaders.TryAddWithoutValidation("Accept", EventStreamMimeType);
Hc.DefaultRequestHeaders.TryAddWithoutValidation("Last-Event-Id", LastEventId);
}
using (HttpResponseMessage response = await Hc.GetAsync(Uri, HttpCompletionOption.ResponseHeadersRead))
{
response.EnsureSuccessStatusCode();
if (response.Headers.TryGetValues("content-type", out IEnumerable<string> ctypes) || ctypes?.Contains("text/event-stream") == false)
if (response.Headers.TryGetValues("content-type", out IEnumerable<string> ctypes) || ctypes?.Contains(EventStreamMimeType) == false)
{
throw new ArgumentException("Specified URI does not return server-sent events");
}
Expand All @@ -109,7 +110,7 @@ private async Task ReaderAsync()
string id = string.Empty;
var data = new StringBuilder(string.Empty);

while (true)
while (!sr.EndOfStream)
{
string line = await sr.ReadLineAsync();
if (line == string.Empty)
Expand Down Expand Up @@ -169,6 +170,7 @@ private async Task ReaderAsync()
break;
}
}
Disconnect(new Exception("stream ended. maybe timeout"));
}
}
}
Expand Down