Skip to content

Commit f9fc787

Browse files
authored
+83 -0 main.go and service.go (#32)
1 parent 1fa6b6e commit f9fc787

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

cmd/server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func main() {
6767
r.HandleFunc("/eventstreams/differentdataschemas", eventstreams.HandleEventStreamDifferentDataSchemas).Methods(http.MethodPost)
6868
r.HandleFunc("/eventstreams/differentdataschemas-flat", eventstreams.HandleEventStreamDifferentDataSchemasFlatten).Methods(http.MethodPost)
6969
r.HandleFunc("/eventstreams/stayopen", eventstreams.HandleEventStreamStayOpen).Methods(http.MethodPost)
70+
r.HandleFunc("/eventstreams/partial-with-comments", eventstreams.HandleEventStreamPartialWithComments).Methods(http.MethodPost)
7071
r.HandleFunc("/jsonl", jsonLines.HandleJSONLinesRich).Methods(http.MethodGet)
7172
r.HandleFunc("/jsonl/deserialization_verification", jsonLines.HandleJsonLinesDeserializationVerification).Methods(http.MethodGet)
7273
r.HandleFunc("/jsonl/chunks", jsonLines.HandleJSONLinesChunksRich).Methods(http.MethodGet)

internal/eventstreams/service.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,85 @@ func HandleEventStreamStayOpen(rw http.ResponseWriter, r *http.Request) {
264264
// Monitor the request context to detect when client disconnects
265265
<-r.Context().Done()
266266
}
267+
268+
func HandleEventStreamPartialWithComments(rw http.ResponseWriter, _ *http.Request) {
269+
rw.Header().Add("Content-Type", "text/event-stream")
270+
271+
// Send the first packet with a partial message and a comment
272+
fmt.Fprint(rw, ": This is a comment\n")
273+
fmt.Fprint(rw, "data: {\"message\": \"Hello ")
274+
275+
if f, ok := rw.(http.Flusher); ok {
276+
f.Flush()
277+
}
278+
279+
time.Sleep(100 * time.Millisecond)
280+
281+
// Complete the first message with LF,LF boundary and add another comment
282+
fmt.Fprint(rw, "from SSE\"}\n\n")
283+
fmt.Fprint(rw, ": Another comment line\n")
284+
285+
if f, ok := rw.(http.Flusher); ok {
286+
f.Flush()
287+
}
288+
289+
time.Sleep(100 * time.Millisecond)
290+
291+
// Send a complete event with CR,CR boundary
292+
fmt.Fprint(rw, "id: msg-2\n")
293+
fmt.Fprint(rw, "event: update\n")
294+
fmt.Fprint(rw, ": Comment before data\n")
295+
fmt.Fprint(rw, "data: {\"status\": \"processing\", \"progress\": 50}\r\r")
296+
297+
if f, ok := rw.(http.Flusher); ok {
298+
f.Flush()
299+
}
300+
301+
time.Sleep(100 * time.Millisecond)
302+
303+
// Send with CR,LF,CR,LF boundary
304+
fmt.Fprint(rw, ": This is a multiline\r\n")
305+
fmt.Fprint(rw, ": comment that spans\r\n")
306+
fmt.Fprint(rw, ": multiple lines\r\n")
307+
fmt.Fprint(rw, "id: msg-3\r\n")
308+
fmt.Fprint(rw, "data: {\"status\": \"complete\",\r\n")
309+
fmt.Fprint(rw, "data: \"progress\": 100,\r\n")
310+
fmt.Fprint(rw, "data: \"result\": \"Success\"}\r\n\r\n")
311+
312+
if f, ok := rw.(http.Flusher); ok {
313+
f.Flush()
314+
}
315+
316+
time.Sleep(100 * time.Millisecond)
317+
318+
// Mix boundaries within same message group - CR for lines, LF,LF for message end
319+
fmt.Fprint(rw, ": Mixed line endings\r")
320+
fmt.Fprint(rw, "event: mixed\n")
321+
fmt.Fprint(rw, "id: msg-4\r")
322+
fmt.Fprint(rw, "data: {\"test\": \"mixed boundaries\"}\n\n")
323+
324+
if f, ok := rw.(http.Flusher); ok {
325+
f.Flush()
326+
}
327+
328+
time.Sleep(100 * time.Millisecond)
329+
330+
// Another variant with CR,CR ending
331+
fmt.Fprint(rw, "data: {\"another\": \"test\"}\r")
332+
fmt.Fprint(rw, ": Comment with CR\r")
333+
fmt.Fprint(rw, "id: msg-5\r\r")
334+
335+
if f, ok := rw.(http.Flusher); ok {
336+
f.Flush()
337+
}
338+
339+
time.Sleep(100 * time.Millisecond)
340+
341+
// Send a final comment and done signal with standard LF,LF
342+
fmt.Fprint(rw, ": Stream ending\n")
343+
fmt.Fprint(rw, "data: [DONE]\n\n")
344+
345+
if f, ok := rw.(http.Flusher); ok {
346+
f.Flush()
347+
}
348+
}

0 commit comments

Comments
 (0)