@@ -264,3 +264,85 @@ func HandleEventStreamStayOpen(rw http.ResponseWriter, r *http.Request) {
264
264
// Monitor the request context to detect when client disconnects
265
265
<- r .Context ().Done ()
266
266
}
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