Exemplo n.º 1
0
func (w *Writer) handle(rw http.ResponseWriter, r *http.Request) {
	w.readers.Add(1)
	defer w.readers.Done()

	if r.RequestURI == "/ready" {
		rw.WriteHeader(http.StatusOK)
		return
	}

	rw.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
	rw.Header().Set("Cache-Control", "no-cache")
	rw.Header().Set("Connection", "keep-alive")
	rw.WriteHeader(http.StatusOK)
	rw.Write([]byte("\n"))
	flusher := rw.(http.Flusher)
	flusher.Flush()

	closeNotifier := rw.(http.CloseNotifier).CloseNotify()

	for {
		select {
		case <-closeNotifier:
			return
		case message, ok := <-w.messages:
			if !ok {
				return
			}

			event := vitosse.Event{
				Data: message,
			}

			err := event.Write(rw)
			flusher.Flush()

			w.responses <- response{
				count: len(message),
				err:   err,
			}
		}
	}
}
Exemplo n.º 2
0
					close(streaming)

					id := 0

					for e := range events {
						payload, err := json.Marshal(event.Message{Event: e})
						Expect(err).NotTo(HaveOccurred())

						event := sse.Event{
							ID:   fmt.Sprintf("%d", id),
							Name: "event",
							Data: payload,
						}

						err = event.Write(w)
						Expect(err).NotTo(HaveOccurred())

						flusher.Flush()

						id++
					}

					err := sse.Event{
						Name: "end",
					}.Write(w)
					Expect(err).NotTo(HaveOccurred())
				},
			),
		)
		atcServer.RouteToHandler("PUT", "/api/v1/pipes/some-pipe-id",
Exemplo n.º 3
0
		BeforeEach(func() {
			data, _ := json.Marshal(route1)
			event = sse.Event{
				ID:   "1",
				Name: "Upsert",
				Data: data,
			}

			server.AppendHandlers(
				ghttp.CombineHandlers(
					ghttp.VerifyRequest("GET", "/v1/events"),
					ghttp.VerifyHeader(http.Header{
						"Authorization": []string{"bearer"},
					}),
					func(w http.ResponseWriter, req *http.Request) {
						event.Write(w)
					},
				),
			)
		})

		JustBeforeEach(func() {
			eventSource, err = client.SubscribeToEvents()
		})

		It("Starts an SSE connection to the server", func() {
			Expect(server.ReceivedRequests()).Should(HaveLen(1))
		})

		It("Streams events from the server", func() {
			Expect(err).NotTo(HaveOccurred())