Beispiel #1
0
func generateRandomEvents(lpManager *golongpoll.LongpollManager) {
	farm_events := []string{
		"Cow says 'Moooo!'",
		"Duck went 'Quack!'",
		"Chicken says: 'Cluck!'",
		"Goat chewed grass.",
		"Pig went 'Oink! Oink!'",
		"Horse ate hay.",
		"Tractor went: Vroom Vroom!",
		"Farmer ate bacon.",
	}
	// every 0-5 seconds, something happens at the farm:
	for {
		time.Sleep(time.Duration(rand.Intn(5000)) * time.Millisecond)
		lpManager.Publish("farm", farm_events[rand.Intn(len(farm_events))])
	}
}
Beispiel #2
0
func notifyProxyEvent(action string, req *http.Request, lpManager *golongpoll.LongpollManager) {
	// in the event localhost isn't added to noproxy, don't emit localhost event
	normUrl := strings.ToLower(req.URL.String())
	if strings.HasPrefix(normUrl, "http://127.0.0.1:") ||
		strings.HasPrefix(normUrl, "http://127.0.0.1/") ||
		strings.HasPrefix(normUrl, "127.0.0.1:") ||
		strings.HasPrefix(normUrl, "127.0.0.1/") {
		// no events for you!
		return
	}
	var category string
	if referer := req.Header.Get("Referer"); len(referer) > 0 {
		category = utils.StripProxyExceptionStringFromUrl(referer)
	} else {
		category = utils.StripProxyExceptionStringFromUrl(req.URL.String())
	}
	eventData := action + ": " + req.URL.String()
	if err := lpManager.Publish(category, eventData); err != nil {
		log.Printf("ERROR: failed to publish event.  error: %q", err)
	}
}
Beispiel #3
0
// Creates a closure function that is used as an http handler that allows
// users to publish events (what this example is calling a user action event)
func getUserActionHandler(manager *golongpoll.LongpollManager) func(w http.ResponseWriter, r *http.Request) {
	// Creates closure that captures the LongpollManager
	return func(w http.ResponseWriter, r *http.Request) {
		user := r.URL.Query().Get("user")
		action := r.URL.Query().Get("action")
		public := r.URL.Query().Get("public")
		// Perform validation on url query params:
		if len(user) == 0 || len(action) == 0 {
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte("Missing required URL param."))
			return
		}
		if user != "larry" && user != "moe" && user != "curly" {
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte("Not a user."))
			return
		}
		if len(public) > 0 && public != "true" {
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte("Optional param 'public' must be 'true' if present."))
			return
		}
		// convert string arg to bool
		isPublic := false
		if public == "true" {
			isPublic = true
		}
		actionEvent := UserAction{User: user, Action: action, IsPublic: isPublic}
		// Publish on public subscription channel if the action is public.
		// Everyone can see this event.
		if isPublic {
			manager.Publish("public_actions", actionEvent)
		}
		// Publish on user's private channel regardless
		// Only the user that called this will see the event.
		manager.Publish(user+"_actions", actionEvent)
	}
}