Пример #1
0
// Stream the messages for the given flow.
//
// Flowdock API docs: https://flowdock.com/api/streaming and
// https://www.flowdock.com/api/messages
func (s *MessagesService) Stream(token, org, flow string) (chan Message, *eventsource.EventSource, error) {
	retryDuration := 3 * time.Second

	u := fmt.Sprintf("flows/%v/%v?access_token=%v", org, flow, token)

	req, err := s.client.NewStreamRequest("GET", u, nil)
	if err != nil {
		return nil, nil, err
	}

	messageCh := make(chan Message)
	es := eventsource.New(req, retryDuration)

	go func() {
		for {
			event, err := es.Read()

			if err != nil {
				// TODO panic or add error channel!
			}

			m := new(Message)
			err = json.Unmarshal([]byte(event.Data), m)
			messageCh <- *m
		}
	}()

	return messageCh, es, err
}
Пример #2
0
func ExampleNew() {
	req, _ := http.NewRequest("GET", "http://localhost:9090/events", nil)
	req.SetBasicAuth("user", "pass")

	es := eventsource.New(req, 3*time.Second)

	for {
		event, err := es.Read()

		if err != nil {
			log.Fatal(err)
		}

		log.Printf("%s. %s %s\n", event.ID, event.Type, event.Data)
	}
}