示例#1
0
func (s *Server) subHandler(w http.ResponseWriter, r *http.Request) {
	queues_param := r.FormValue("queues")
	var queues []string
	if len(queues_param) > 0 {
		queues = strings.Split(queues_param, ",")
	}

	sub := hub.NewSubscription(queues)

	finished := make(chan struct{})
	defer close(finished)

	disconnected := w.(http.CloseNotifier).CloseNotify()
	go func() {
		select {
		case <-disconnected:
		case <-finished:
		}
		sub.Close()
	}()

	go s.hub.Sub(sub)

	if res, ok := <-sub.Result(); ok {
		w.Header().Set("Queue", res.Queue)
		w.Write(res.Message)
		counts.Add("SubCount", 1)
		counts.Add("SubBs", int64(len(res.Message)))
	}
}
示例#2
0
func (s *Server) subHandler(w http.ResponseWriter, r *http.Request) {
	queues := strings.Split(r.FormValue("queues"), ",")
	sub := hub.NewSubscription(queues)

	finished := make(chan struct{})
	defer close(finished)

	disconnected := w.(http.CloseNotifier).CloseNotify()
	go func() {
		select {
		case <-disconnected:
		case <-finished:
		}
		sub.Close()
	}()

	go s.hub.Sub(sub)

	if res, ok := <-sub.Result(); ok {
		w.Header().Set("Queue", res.Queue)
		w.Write(res.Message)
	}
}