Skip to content

Instantly share code, notes, and snippets.

Created March 31, 2013 18:40
Show Gist options
  • Save anonymous/5281559 to your computer and use it in GitHub Desktop.
Save anonymous/5281559 to your computer and use it in GitHub Desktop.
text/event-stream response for NancyFx
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