-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
63 additions
and
54 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package sse | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// EventWriter is a helper for writing events to an io.Writer stream. | ||
type EventWriter struct { | ||
w http.ResponseWriter | ||
} | ||
|
||
// NewEventWriter returns a new EventWriter. | ||
func NewEventWriter(w http.ResponseWriter) *EventWriter { | ||
w.Header().Set(contentTypeHeader, textEventStream) | ||
return &EventWriter{w: w} | ||
} | ||
|
||
// Write takes a io.Writer and an Event, serializes the Event to bytes, and writes them out to the | ||
// writer. If the writer is also an http.Flusher, then it will also flush the bytes out so the | ||
// event will be sent over the wire immediately. It automatically sets the Content-Type header to | ||
// text/event-stream. | ||
func (evw *EventWriter) Write(e Event) error { | ||
|
||
if _, err := evw.w.Write(e.Bytes()); err != nil { | ||
return err | ||
} | ||
if flusher, ok := evw.w.(http.Flusher); ok { | ||
flusher.Flush() | ||
} | ||
return nil | ||
} |
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