Exemple #1
0
func newReader(w http.ResponseWriter, r *http.Request) (io.ReadCloser, error) {
	rd, err := newStorageReader(w, r)
	if err != nil {
		if rd != nil {
			rd.Close()
		}
		return rd, err
	}

	// For default requests, we use a null byte for sending
	// the keepalive ack.
	ack := util.GetNullByte()

	if broker.NoContent(rd, offset(r)) {
		return nil, errNoContent
	}

	if r.Header.Get("Accept") == "text/event-stream" {
		w.Header().Set("Content-Type", "text/event-stream")
		w.Header().Set("Cache-Control", "no-cache")

		encoder := sse.NewEncoder(rd)
		encoder.(io.Seeker).Seek(offset(r), 0)

		rd = ioutil.NopCloser(encoder)

		// For SSE, we change the ack to a :keepalive
		ack = []byte(":keepalive\n")
	}

	done := w.(http.CloseNotifier).CloseNotify()
	return newKeepAliveReader(rd, ack, *util.HeartbeatDuration, done), nil
}
Exemple #2
0
func (s *Server) newReader(w http.ResponseWriter, r *http.Request) (io.ReadCloser, error) {
	rd, err := s.newStorageReader(w, r)
	if err != nil {
		if rd != nil {
			rd.Close()
		}
		return rd, err
	}

	// For default requests, we use a null byte for sending
	// the keepalive ack.
	ack := []byte{0}

	o, err := offset(r)
	if err != nil {
		return nil, err
	}

	if broker.NoContent(rd, o) {
		return nil, errNoContent
	}

	var encoder io.ReadSeeker
	if r.Header.Get("Accept") == "text/event-stream" {
		w.Header().Set("Content-Type", "text/event-stream")
		w.Header().Set("Cache-Control", "no-cache")

		encoder = encoders.NewSSEEncoder(rd)
		encoder.(io.Seeker).Seek(o, 0)

		// For SSE, we change the ack to a :keepalive
		ack = []byte(":keepalive\n")
	} else {
		encoder = encoders.NewTextEncoder(rd)
	}

	encoder.Seek(o, io.SeekStart)
	rd = ioutil.NopCloser(encoder)

	done := w.(http.CloseNotifier).CloseNotify()
	return newKeepAliveReader(rd, ack, s.HeartbeatDuration, done), nil
}