Skip to content

Commit aedfd50

Browse files
authored
Merge pull request #9 from launchdarkly/eb/ch81628/sse-comment
add comment methods to SSEStreamControl
2 parents ee97047 + 9f8bbf2 commit aedfd50

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ clean:
1313
go clean
1414

1515
test: build
16-
@# Note, we need to specify all these packages individually for go test in order to remain 1.8-compatible
17-
go test -race -v . ./httphelpers ./ldservices
16+
go test -race -v ./...
1817

1918
$(LINTER_VERSION_FILE):
2019
rm -f $(LINTER)
2120
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s $(GOLANGCI_LINT_VERSION)
2221
touch $(LINTER_VERSION_FILE)
2322

2423
lint: $(LINTER_VERSION_FILE)
25-
go get ./...
2624
$(LINTER) run ./...

httphelpers/handlers_sse.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ type SSEStreamControl interface {
4141
// event is sent to all of them. If there are no open connections, the event is discarded.
4242
Send(event SSEEvent)
4343

44+
// EnqueueComment is the same as Enqueue, except that it sends a comment line instead of an
45+
// event. A colon is prepended to the comment.
46+
EnqueueComment(comment string)
47+
48+
// SendComment is the same as Send, except that it sends a comment line instead of an event.
49+
// A colon is prepended to the comment.
50+
SendComment(comment string)
51+
4452
// EndAll terminates any existing connections to this endpoint, but allows new connections
4553
// afterward.
4654
EndAll()
@@ -99,6 +107,14 @@ func (s *sseStreamControlImpl) Send(event SSEEvent) {
99107
s.streamControl.Send(event.Bytes())
100108
}
101109

110+
func (s *sseStreamControlImpl) EnqueueComment(comment string) {
111+
s.streamControl.Enqueue([]byte(fmt.Sprintf(":%s\n", comment)))
112+
}
113+
114+
func (s *sseStreamControlImpl) SendComment(comment string) {
115+
s.streamControl.Send([]byte(fmt.Sprintf(":%s\n", comment)))
116+
}
117+
102118
func (s *sseStreamControlImpl) EndAll() {
103119
s.streamControl.EndAll()
104120
}

httphelpers/handlers_sse_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func TestSSEHandler(t *testing.T) {
1616
defer stream.Close()
1717

1818
stream.Enqueue(SSEEvent{"", "event2", "data2"})
19+
stream.EnqueueComment("comment1")
1920
stream.Send(SSEEvent{"", "", "this isn't sent becauset here are no connections"})
2021

2122
WithServer(handler, func(server *httptest.Server) {
@@ -26,6 +27,7 @@ func TestSSEHandler(t *testing.T) {
2627
assert.Equal(t, 200, resp1.StatusCode)
2728
assert.Equal(t, "text/event-stream; charset=utf-8", resp1.Header.Get("Content-Type"))
2829

30+
stream.SendComment("comment2")
2931
stream.Enqueue(SSEEvent{"", "event3", "data3"})
3032
stream.EndAll()
3133

@@ -39,6 +41,8 @@ data: data1
3941
event: event2
4042
data: data2
4143
44+
:comment1
45+
:comment2
4246
event: event3
4347
data: data3
4448

0 commit comments

Comments
 (0)