@@ -224,3 +224,43 @@ func HandleEventStreamDifferentDataSchemasFlatten(rw http.ResponseWriter, _ *htt
224
224
},
225
225
})
226
226
}
227
+
228
+ func HandleEventStreamStayOpen (rw http.ResponseWriter , r * http.Request ) {
229
+ rw .Header ().Add ("Content-Type" , "text/event-stream" )
230
+ rw .Header ().Add ("Cache-Control" , "no-cache" )
231
+ rw .Header ().Add ("Connection" , "keep-alive" )
232
+
233
+ // Send events 1, 2, 3 immediately
234
+ fmt .Fprintln (rw , "data: event 1" )
235
+ fmt .Fprintln (rw , "" )
236
+ fmt .Fprintln (rw , "data: event 2" )
237
+ fmt .Fprintln (rw , "" )
238
+ fmt .Fprintln (rw , "data: event 3" )
239
+ fmt .Fprintln (rw , "" )
240
+
241
+ if f , ok := rw .(http.Flusher ); ok {
242
+ f .Flush ()
243
+ }
244
+
245
+ // Wait 100ms then send event 4
246
+ time .Sleep (100 * time .Millisecond )
247
+ fmt .Fprintln (rw , "data: event 4" )
248
+ fmt .Fprintln (rw , "" )
249
+
250
+ if f , ok := rw .(http.Flusher ); ok {
251
+ f .Flush ()
252
+ }
253
+
254
+ // Wait another 100ms then send sentinel event
255
+ time .Sleep (100 * time .Millisecond )
256
+ fmt .Fprintln (rw , "data: [SENTINEL]" )
257
+ fmt .Fprintln (rw , "" )
258
+
259
+ if f , ok := rw .(http.Flusher ); ok {
260
+ f .Flush ()
261
+ }
262
+
263
+ // Keep the connection open until client closes
264
+ // Monitor the request context to detect when client disconnects
265
+ <- r .Context ().Done ()
266
+ }
0 commit comments