Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion sse-decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package sse
import (
"bytes"
"io"
"strconv"
)

type decoder struct {
Expand Down Expand Up @@ -99,7 +100,10 @@ func (d *decoder) decode(r io.Reader) ([]Event, error) {
// then interpret the field value as an integer in base ten, and set the event stream's
// reconnection time to that integer.
// Otherwise, ignore the field.
currentEvent.Id = string(value)
if len(value) != 0 && '0' <= value[0] && value[0] <= '9' {
num, _ := strconv.ParseUint(string(value), 10, 0)
currentEvent.Retry = uint(num)
}
case "data":
// Append the field value to the data buffer,
dataBuffer.Write(value)
Expand Down
20 changes: 20 additions & 0 deletions sse-decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ data
assert.Equal(t, events[0].Id, "123456ABCabc789010")
}

func TestDecodeSingle4(t *testing.T) {
events, err := Decode(bytes.NewBufferString(
`
id:123456ABCabc789010
retry: 6
event: message123
: we can append data
data:this is a text
data: a very nice one
data:
data
: ending with a comment`))

assert.NoError(t, err)
assert.Len(t, events, 1)
assert.Equal(t, events[0].Event, "message123")
assert.Equal(t, events[0].Id, "123456ABCabc789010")
assert.Equal(t, events[0].Retry, uint(6))
}

func TestDecodeMulti1(t *testing.T) {
events, err := Decode(bytes.NewBufferString(
`
Expand Down