當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Event.Write方法代碼示例

本文整理匯總了Golang中github.com/vito/go-sse/sse.Event.Write方法的典型用法代碼示例。如果您正苦於以下問題:Golang Event.Write方法的具體用法?Golang Event.Write怎麽用?Golang Event.Write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/vito/go-sse/sse.Event的用法示例。


在下文中一共展示了Event.Write方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: handle

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,
			}
		}
	}
}
開發者ID:myshkin5,項目名稱:netspel,代碼行數:42,代碼來源:writer.go

示例2:

					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",
開發者ID:ArthurHlt,項目名稱:fly,代碼行數:30,代碼來源:execute_test.go

示例3:

		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())
開發者ID:jungle0755,項目名稱:gorouter,代碼行數:31,代碼來源:client_test.go


注:本文中的github.com/vito/go-sse/sse.Event.Write方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。