Skip to content

Instantly share code, notes, and snippets.

@serbrech
Forked from anonymous/EventStreamResponse.cs
Created March 31, 2013 18:41
Show Gist options
  • Save serbrech/5281563 to your computer and use it in GitHub Desktop.
Save serbrech/5281563 to your computer and use it in GitHub Desktop.
public class EventStreamResponse : Response
{
public EventStreamResponse(IResponseFormatter formatter, string id, dynamic body)
{
var serializer = formatter.Serializers.FirstOrDefault(s => s.CanSerialize("application/json"));
ContentType = "text/event-stream";
Contents = s =>
{
var idBytes = Encoding.UTF8.GetBytes(id + "\n");
s.Write(idBytes, 0, idBytes.Length);
serializer.Serialize("application/json", body, s);
s.Flush();
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment