Exemplo n.º 1
0
func ExampleEvent() {
	srv := eventsource.NewServer()
	defer srv.Close()
	l, err := net.Listen("tcp", ":8080")
	if err != nil {
		return
	}
	defer l.Close()
	http.HandleFunc("/time", srv.Handler("time"))
	go http.Serve(l, nil)
	go TimePublisher(srv)
	stream, err := eventsource.Subscribe("http://127.0.0.1:8080/time", "")
	if err != nil {
		return
	}
	for i := 0; i < TICK_COUNT; i++ {
		ev := <-stream.Events
		fmt.Println(ev.Id(), ev.Event(), ev.Data())
	}

	// Output:
	// 1356998400000000000 Tick 2013-01-01 00:00:00 +0000 UTC
	// 1356998401000000000 Tick 2013-01-01 00:00:01 +0000 UTC
	// 1356998402000000000 Tick 2013-01-01 00:00:02 +0000 UTC
	// 1356998403000000000 Tick 2013-01-01 00:00:03 +0000 UTC
	// 1356998404000000000 Tick 2013-01-01 00:00:04 +0000 UTC
}
func ExampleErrorHandlingStream() {
	listener, err := net.Listen("tcp", ":8080")
	if err != nil {
		return
	}
	defer listener.Close()
	http.HandleFunc("/stream", func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "Something wrong.", 500)
	})
	go http.Serve(listener, nil)

	_, err = eventsource.Subscribe("http://127.0.0.1:8080/stream", "")
	if err != nil {
		if serr, ok := err.(eventsource.SubscriptionError); ok {
			fmt.Printf("Status code: %d\n", serr.Code)
			fmt.Printf("Message: %s\n", serr.Message)
		} else {
			fmt.Println("failed to subscribe")
		}
	}

	// Output:
	// Status code: 500
	// Message: Something wrong.
}
Exemplo n.º 3
0
func MonitorFeeds(reg *registry.Registry) {
	if reg.Feeds == "" {
		return
	}
	f, err := os.Open(reg.Feeds)
	if err != nil {
		glog.Fatalln("Reading feeds:", err)
	}
	defer f.Close()
	var feeds []Feed
	if err := json.NewDecoder(f).Decode(&feeds); err != nil {
		glog.Fatalln("Decoding feeds:", err)
	}
	db := reg.DB()
	defer db.Session.Close()
	for i := range feeds {
		if err := db.C("feeds").FindId(feeds[i].DocType).One(&feeds[i]); err != nil && err != mgo.ErrNotFound {
			glog.Fatalln("Finding existing feeds:", err)
		}
		feeds[i].stream, err = eventsource.Subscribe(feeds[i].Url, feeds[i].LastEventId)
		if err == nil {
			glog.Infof("Monitoring: %s", &feeds[i])
			go monitor(reg, &feeds[i])
		} else {
			glog.Fatalln("Eventsource:", err)
		}
	}
}
func ExampleRepository() {
	srv := eventsource.NewServer()
	defer srv.Close()
	http.HandleFunc("/articles", srv.Handler("articles"))
	l, err := net.Listen("tcp", ":8080")
	if err != nil {
		return
	}
	defer l.Close()
	go http.Serve(l, nil)
	stream, err := eventsource.Subscribe("http://127.0.0.1:8080/articles", "")
	if err != nil {
		return
	}
	go buildRepo(srv)
	// This will receive events in the order that they come
	for i := 0; i < 3; i++ {
		ev := <-stream.Events
		fmt.Println(ev.Id(), ev.Event(), ev.Data())
	}
	stream, err = eventsource.Subscribe("http://127.0.0.1:8080/articles", "1")
	if err != nil {
		fmt.Println(err)
		return
	}
	// This will replay the events in order of id
	for i := 0; i < 3; i++ {
		ev := <-stream.Events
		fmt.Println(ev.Id(), ev.Event(), ev.Data())
	}
	// Output:
	// 2 News Article {"Title":"Governments struggle to control global price of gas","Content":"Hot air...."}
	// 1 News Article {"Title":"Tomorrow is another day","Content":"And so is the day after."}
	// 3 News Article {"Title":"News for news' sake","Content":"Nothing has happened."}
	// 1 News Article {"Title":"Tomorrow is another day","Content":"And so is the day after."}
	// 2 News Article {"Title":"Governments struggle to control global price of gas","Content":"Hot air...."}
	// 3 News Article {"Title":"News for news' sake","Content":"Nothing has happened."}
}
Exemplo n.º 5
0
func (r *marathonClient) registerSSESubscription() error {
	// Prevent multiple SSE subscriptions
	if r.subscribedToSSE {
		return nil
	}

	var stream *eventsource.Stream

	// Try to connect to Marathon until succeed or
	// the whole custer is down
	for {
		// Get a member from the cluster
		marathon, err := r.cluster.GetMember()
		if err != nil {
			return err
		}
		url := fmt.Sprintf("%s/%s", marathon, marathonAPIEventStream)

		// Try to connect to stream
		stream, err = eventsource.Subscribe(url, "")
		if err == nil {
			break
		}

		glog.V(debugLevel).Infof("failed to connect to Marathon event stream, error: %s", err)
		r.cluster.MarkDown()
	}

	go func() {
		for {
			select {
			case ev := <-stream.Events:
				r.handleEvent(ev.Data())
			case err := <-stream.Errors:
				glog.V(debugLevel).Infof("failed to receive event, error: %s", err)
			}
		}
	}()

	r.subscribedToSSE = true
	return nil
}
func (r *marathonClient) registerSSESubscription() error {
	// Prevent multiple SSE subscriptions
	if r.subscribedToSSE {
		return nil
	}

	// Get a member from the cluster
	marathon, err := r.cluster.GetMember()
	if err != nil {
		return err
	}
	url := fmt.Sprintf("%s/%s", marathon, marathonAPIEventStream)

	// Try to connect to stream
	stream, err := eventsource.Subscribe(url, "")
	if err != nil {
		return err
	}

	go func() {
		for {
			select {
			case ev := <-stream.Events:
				if err := r.handleEvent(ev.Data()); err != nil {
					// TODO let the user handle this error instead of logging it here
					r.debugLog.Printf("registerSSESubscription(): failed to handle event: %v\n", err)
				}
			case err := <-stream.Errors:
				// TODO let the user handle this error instead of logging it here
				r.debugLog.Printf("registerSSESubscription(): failed to receive event: %v\n", err)
			}
		}
	}()

	r.subscribedToSSE = true
	return nil
}
Exemplo n.º 7
0
	DeviceID    string
	AccessToken string
	APIServer   string
	servoPins   map[string]bool
	gobot.Eventer
}

// Event is an event emitted by the Particle cloud
type Event struct {
	Name  string
	Data  string
	Error error
}

var eventSource = func(url string) (chan eventsource.Event, chan error, error) {
	stream, err := eventsource.Subscribe(url, "")
	if err != nil {
		return nil, nil, err
	}
	return stream.Events, stream.Errors, nil
}

// NewAdaptor creates new Photon adaptor with deviceId and accessToken
// using api.particle.io server as default
func NewAdaptor(deviceID string, accessToken string) *Adaptor {
	return &Adaptor{
		name:        "Particle",
		DeviceID:    deviceID,
		AccessToken: accessToken,
		servoPins:   make(map[string]bool),
		APIServer:   "https://api.particle.io",