Created
March 31, 2013 18:40
-
-
Save anonymous/5281559 to your computer and use it in GitHub Desktop.
text/event-stream response for NancyFx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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