Esempio n. 1
0
func streamOutput(c *gin.Context, output *streams.Output, replay bool) {
	c.Writer.WriteHeader(http.StatusOK)
	c.Writer.Header().Set("Cache-Control", "no-cache")
	c.Writer.Header().Set("Connection", "keep-alive")

	var w io.Writer
	if c.Request.Header.Get("Accept") == "text/event-stream" {
		c.Writer.Header().Set("Content-Type", "text/event-stream")
		w = NewSSEWriter(c.Writer)
	} else {
		c.Writer.Header().Set("Content-Type", "text/plain")
		w = flushwriter.New(c.Writer)
	}

	waitChan := make(chan bool, 1)
	notify := c.Writer.CloseNotify()

	go func() {
		<-notify
		waitChan <- true
	}()

	if replay {
		output.Replay(w)
	}
	output.Add(w, waitChan)
	<-waitChan
	output.Remove(w)
}
Esempio n. 2
0
func NewSSEWriter(w io.Writer) SSEWriter {
	return SSEWriter{writer: flushwriter.New(w)}
}