File tree Expand file tree Collapse file tree 2 files changed +9
-4
lines changed Expand file tree Collapse file tree 2 files changed +9
-4
lines changed Original file line number Diff line number Diff line change @@ -20,8 +20,11 @@ type Client struct {
20
20
// NewClient creates a client wrapping a response writer.
21
21
// The response writer must support http.Flusher and http.CloseNotifier
22
22
// interfaces.
23
+ // When writing, the client will automatically send some headers. Passing the
24
+ // original http.Request helps determine which headers, but the request it is
25
+ // optional.
23
26
// Returns nil on error.
24
- func NewClient (w http.ResponseWriter ) * Client {
27
+ func NewClient (w http.ResponseWriter , req * http. Request ) * Client {
25
28
c := & Client {
26
29
events : make (chan * Event , 1 ),
27
30
write : w ,
@@ -44,7 +47,9 @@ func NewClient(w http.ResponseWriter) *Client {
44
47
// Send the initial headers
45
48
w .Header ().Set ("Content-Type" , "text/event-stream" )
46
49
w .Header ().Set ("Cache-Control" , "no-cache" )
47
- w .Header ().Set ("Connection" , "keep-alive" )
50
+ if req == nil || req .ProtoMajor < 2 {
51
+ w .Header ().Set ("Connection" , "keep-alive" )
52
+ }
48
53
flush .Flush ()
49
54
50
55
// start the sending thread
Original file line number Diff line number Diff line change @@ -161,7 +161,7 @@ func (s *Stream) ServeHTTP(w http.ResponseWriter, r *http.Request) {
161
161
}
162
162
163
163
// create the client
164
- c := NewClient (w )
164
+ c := NewClient (w , r )
165
165
if c == nil {
166
166
http .Error (w , "EventStream not supported for this connection" , http .StatusInternalServerError )
167
167
return
@@ -188,7 +188,7 @@ func (s *Stream) TopicHandler(topics []string) http.HandlerFunc {
188
188
}
189
189
190
190
// create the client
191
- c := NewClient (w )
191
+ c := NewClient (w , r )
192
192
if c == nil {
193
193
http .Error (w , "EventStream not supported for this connection" , http .StatusInternalServerError )
194
194
return
You can’t perform that action at this time.
0 commit comments