InitiateStream initiates the SSE stream by sending headers and starting the ping ticker. This is safe to call multiple times as only the first call has any effect.
(w http.ResponseWriter)
| 65 | // ping ticker. This is safe to call multiple times as only the first call has |
| 66 | // any effect. |
| 67 | func (s *EventStream) InitiateStream(w http.ResponseWriter) { |
| 68 | s.initiateOnce.Do(func() { |
| 69 | s.initiated.Store(true) |
| 70 | s.logger.Debug(s.ctx, "stream initiated") |
| 71 | |
| 72 | // Send headers for Server-Sent Event stream. |
| 73 | w.Header().Set("Content-Type", "text/event-stream") |
| 74 | w.Header().Set("Cache-Control", "no-cache") |
| 75 | w.Header().Set("Connection", "keep-alive") |
| 76 | w.Header().Set("X-Accel-Buffering", "no") |
| 77 | |
| 78 | // Send initial flush to ensure connection is established. |
| 79 | if err := flush(w); err != nil { |
| 80 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | // Start ping ticker. |
| 85 | s.tick.Reset(pingInterval) |
| 86 | }) |
| 87 | } |
| 88 | |
| 89 | // Start handles sending Server-Sent Event to the client. |
| 90 | func (s *EventStream) Start(w http.ResponseWriter, r *http.Request) { |
no test coverage detected