예제 #1
0
파일: sse.go 프로젝트: NailClippar/go-web
func SSEHandler(w http.ResponseWriter, r *http.Request) {
	sf := 0
	startFrame := r.FormValue("startFrame")
	if startFrame != "" {
		sf, _ = strconv.Atoi(startFrame)
	}
	if sf < 0 || sf >= cap(frames) {
		sf = 0
	}
	conn, buf, err := sse.ServeEvents(w)
	if err != nil {
		log.Println(err)
		return
	}
	defer conn.Close()
	// Play the movie, frame by frame
	nbytes := 0
	for n, f := range frames[sf:] {
		nbytes += len(f.Buf)
		m := &sse.MessageEvent{Id: strconv.Itoa(n + 1), Data: f.Buf}
		if err = sse.SendEvent(buf, m); err != nil {
			// Usually a broken pipe error.
			// log.Println(e.Error())

			// We update the bytes written to the handler so
			// logging works fine.
			if lw, ok := w.(*httpxtra.LogWriter); ok {
				lw.Bytes += nbytes
			}
			break
		}
		time.Sleep(f.Time)
	}
}
예제 #2
0
파일: sse.go 프로젝트: raul/go-web
func SSEHandler(w http.ResponseWriter, r *http.Request) {
	sf := 0
	startFrame := r.FormValue("startFrame")
	if startFrame != "" {
		sf, _ = strconv.Atoi(startFrame)
	}
	if sf < 0 || sf >= cap(frames) {
		sf = 0
	}
	conn, buf, err := sse.ServeEvents(w)
	if err != nil {
		log.Println(err)
		return
	}
	defer conn.Close()
	// Play the movie, frame by frame
	for n, f := range frames[sf:] {
		m := &sse.MessageEvent{Id: strconv.Itoa(n + 1), Data: f.Buf}
		e := sse.SendEvent(buf, m)
		if e != nil {
			// usually a broken pipe error
			// log.Println(e.Error())
			break
		}
		time.Sleep(f.Time)
	}
}
예제 #3
0
func CometQueueHandler(w http.ResponseWriter, r *http.Request) {
	vars := remux.Vars(r)
	conn, buf, err := sse.ServeEvents(w)
	if err != nil {
		fmt.Println(err)
		http.Error(w, http.StatusText(500), 500)
		return
	}
	defer conn.Close()
	cs := queues.Join(vars[0])
	for s := range cs {
		err = sse.SendEvent(buf, &sse.MessageEvent{Data: s})
		if err != nil {
			break
		}
	}
	queues.Part(vars[0], cs)
}