当前位置: 首页>>代码示例>>Golang>>正文


Golang sse.Event类代码示例

本文整理汇总了Golang中github.com/vito/go-sse/sse.Event的典型用法代码示例。如果您正苦于以下问题:Golang Event类的具体用法?Golang Event怎么用?Golang Event使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Event类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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:

					w.WriteHeader(http.StatusOK)

					flusher.Flush()

					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())
开发者ID:ArthurHlt,项目名称:fly,代码行数:30,代码来源:execute_test.go

示例3:

					Action: "Delete",
					Route: db.Route{
						Route:           "z.a.k",
						Port:            63,
						IP:              "42.42.42.42",
						TTL:             1,
						LogGuid:         "Tomato",
						RouteServiceUrl: "https://route-service-url.com",
					},
				}

				routeString, err := json.Marshal(event.Route)
				Expect(err).ToNot(HaveOccurred())

				sseEvent := sse.Event{
					Name: event.Action,
					Data: routeString,
				}

				headers := make(http.Header)
				headers.Set("Content-Type", "text/event-stream; charset=utf-8")

				command := buildCommand("events", flags, []string{})

				server.SetHandler(0,
					ghttp.CombineHandlers(
						ghttp.VerifyRequest("GET", "/v1/events"),
						ghttp.RespondWith(http.StatusOK, sseEvent.Encode(), headers),
					),
				)

				session := routeRegistrar(command...)
开发者ID:markstgodard,项目名称:routing-api-cli,代码行数:32,代码来源:main_test.go

示例4:

				log := string(r)
				Expect(err).NotTo(HaveOccurred())
				Expect(log).To(ContainSubstring("REQUEST: "))
				Expect(log).To(ContainSubstring("GET /v1/routes HTTP/1.1"))

				Expect(log).To(ContainSubstring("RESPONSE: "))
				Expect(log).To(ContainSubstring("HTTP/1.1 400 Bad Request"))
				Expect(log).NotTo(ContainSubstring(string(expectedBody)))
			})
		})
	})

	Context("SubscribeToEvents", func() {
		var eventSource routing_api.EventSource
		var err error
		var event sse.Event

		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"},
					}),
开发者ID:jungle0755,项目名称:gorouter,代码行数:31,代码来源:client_test.go


注:本文中的github.com/vito/go-sse/sse.Event类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。