Improve messages for events
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2019-03-24 16:41:13 +01:00
parent 2971f45d7d
commit 6fe3e8fd8e
Signed by: peter
GPG Key ID: 374322D56E5209E8
2 changed files with 8 additions and 8 deletions

View File

@ -362,7 +362,7 @@ func (c *Client) Events() (chan sse.Message, error) {
for { for {
res, err := c.microsubGetRequest("events", nil) res, err := c.microsubGetRequest("events", nil)
if err != nil { if err != nil {
log.Printf("could not request events: %s", err) log.Printf("could not request events: %+v", err)
errorCounter++ errorCounter++
if errorCounter > 5 { if errorCounter > 5 {
break break
@ -372,7 +372,7 @@ func (c *Client) Events() (chan sse.Message, error) {
err = sse.Reader(res.Body, ch) err = sse.Reader(res.Body, ch)
if err != nil { if err != nil {
log.Printf("could not create reader: %s", err) log.Printf("could not create reader: %+v", err)
break break
} }
} }

View File

@ -117,11 +117,6 @@ func WriteMessages(w http.ResponseWriter, messageChan chan Message) error {
w.Header().Set("Connection", "keep-alive") w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Origin", "*")
_, err := fmt.Fprintf(w, "event: started\r\n")
if err != nil {
return err
}
var welcomeMsg welcomeMessage var welcomeMsg welcomeMessage
welcomeMsg.Version = "1.0.0" welcomeMsg.Version = "1.0.0"
encoded, err := json.Marshal(&welcomeMsg) encoded, err := json.Marshal(&welcomeMsg)
@ -129,6 +124,11 @@ func WriteMessages(w http.ResponseWriter, messageChan chan Message) error {
return errors.Wrap(err, "could not encode welcome message") return errors.Wrap(err, "could not encode welcome message")
} }
_, err = fmt.Fprintf(w, "event: started\r\n")
if err != nil {
return err
}
_, err = fmt.Fprintf(w, "data: %s", encoded) _, err = fmt.Fprintf(w, "data: %s", encoded)
if err != nil { if err != nil {
return err return err
@ -181,7 +181,7 @@ func Reader(body io.ReadCloser, ch MessageChan) error {
} }
} }
if err := r.Err(); err != nil { if err := r.Err(); err != nil {
return errors.Wrap(err, "could not scan lines from sse events: %+v") return errors.Wrap(err, "could not scan lines from sse events")
} }
return nil return nil
} }