WriteSSE writes the current message in a SSE format into the provided writer. For example, writing to a router.Event: m := Message{Name: "users/create", Data: []byte{...}} m.WriteSSE(e.Response, "yourEventId") e.Flush()
(w io.Writer, eventId string)
| 18 | // m.WriteSSE(e.Response, "yourEventId") |
| 19 | // e.Flush() |
| 20 | func (m *Message) WriteSSE(w io.Writer, eventId string) error { |
| 21 | parts := [][]byte{ |
| 22 | []byte("id:" + eventId + "\n"), |
| 23 | []byte("event:" + m.Name + "\n"), |
| 24 | []byte("data:"), |
| 25 | m.Data, |
| 26 | []byte("\n\n"), |
| 27 | } |
| 28 | |
| 29 | for _, part := range parts { |
| 30 | _, err := w.Write(part) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | return nil |
| 37 | } |