Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

Commit

Permalink
Merge branch 'rc/0.2.1' of github.com:Fallenstedt/twitter-stream into…
Browse files Browse the repository at this point in the history
… rc/0.2.1
  • Loading branch information
Fallenstedt committed Jan 9, 2021
2 parents 56584d1 + 52ef3ff commit 0131f69
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type (
// IStream is the interface that the stream struct implements.
IStream interface {
StartStream()
StartStream() error
StopStream()
GetMessages() *chan interface{}
}
Expand Down Expand Up @@ -43,21 +43,22 @@ func (s *stream) StopStream() {
}

// StartStream makes an HTTP request to twitter and starts streaming tweets to the Messages channel.
func (s *stream) StartStream() {
func (s *stream) StartStream() error {

res, err := s.httpClient.newHttpRequest(&requestOpts{
Method: "GET",
Url: endpoints["stream"],
})

if err != nil {
panic(err)
return err
}

s.reader.setStreamResponseBody(res.Body)

s.group.Add(1)
go s.streamMessages(res)
return nil
}

func (s *stream) streamMessages(res *http.Response) {
Expand All @@ -67,15 +68,16 @@ func (s *stream) streamMessages(res *http.Response) {
for !stopped(s.done) {
data, err := s.reader.readNext()
if err != nil {
return
s.messages <- err
s.StopStream()
break
}
if len(data) == 0 {
// empty keep-alive
continue
}

m := string(data)
// TODO send data or error here
s.messages <- m
}
}

0 comments on commit 0131f69

Please sign in to comment.