Improve event message sending
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2019-08-17 22:13:41 +02:00
parent 46c308619e
commit e9008fe45b
Signed by: peter
GPG Key ID: 374322D56E5209E8

View File

@ -124,16 +124,14 @@ func WriteMessages(w http.ResponseWriter, messageChan chan Message) error {
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\r\n\r\n", encoded)
messageID := 1
_, err = fmt.Fprintf(w, "event: started\r\nid: %d\r\ndata: %s\r\n\r\n", messageID, encoded)
if err != nil {
return err
}
messageID++
flusher.Flush()
// block waiting or messages broadcast on this connection's messageChan
@ -144,15 +142,13 @@ func WriteMessages(w http.ResponseWriter, messageChan chan Message) error {
return errors.Wrap(err, "could not marshal message data")
}
_, err = fmt.Fprintf(w, "event: %s\r\n", message.Event)
_, err = fmt.Fprintf(w, "event: %s\r\nid: %d\r\ndata: %s\r\n\r\n", message.Event, messageID, output)
if err != nil {
return errors.Wrap(err, "could not write message header")
return errors.Wrap(err, "could not write message")
}
_, err = fmt.Fprintf(w, "data: %s\r\n\r\n", output)
if err != nil {
return errors.Wrap(err, "could not write message data")
}
messageID++
flusher.Flush()
}