Skip to content

Commit

Permalink
try older flush method
Browse files Browse the repository at this point in the history
  • Loading branch information
will-wow committed May 22, 2024
1 parent b212414 commit 3f56904
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/web/sse_ex/exgom/sse.gom.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Page() g.Node {
),
),
P(
g.Text("The new elements uses the sse.Connect attribute to connect to a server-side event streaming countdown."),
g.Text("The new elements uses the sse.Connect attribute to connect to a server-sent event streaming countdown."),
),
Pre(
Code(
Expand Down
2 changes: 1 addition & 1 deletion examples/web/sse_ex/extempl/sse.templ
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ templ Page() {
</code>
</pre>
<p>
The new elements uses the sse.Connect attribute to connect to a server-side event streaming countdown.
The new elements uses the sse.Connect attribute to connect to a server-sent event streaming countdown.
</p>
<pre>
<code class="language-go">
Expand Down
2 changes: 1 addition & 1 deletion examples/web/sse_ex/extempl/sse_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions examples/web/sse_ex/sse_ex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sse_ex

import (
"fmt"
"log/slog"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -46,9 +47,16 @@ func (ex *example) countdown(w http.ResponseWriter, r *http.Request) {
}

func (ex *example) feed(w http.ResponseWriter, r *http.Request) {
rc := http.NewResponseController(w)
ctx := r.Context()

flusher, ok := w.(http.Flusher)
if !ok {
slog.Error("flush not supported")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("flush not supported"))
return
}

w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
Expand All @@ -62,7 +70,7 @@ func (ex *example) feed(w http.ResponseWriter, r *http.Request) {
_ = extempl.Message(countMessage).Render(ctx, w)
}
})
_ = rc.Flush()
flusher.Flush()
time.Sleep(time.Second)
}

Expand All @@ -73,7 +81,7 @@ func (ex *example) feed(w http.ResponseWriter, r *http.Request) {
_ = extempl.Message("Blastoff!").Render(ctx, w)
}
})
_ = rc.Flush()
flusher.Flush()
time.Sleep(2 * time.Second)

ex.sendEvent(w, shared.ResetEvent, func() {
Expand Down

0 comments on commit 3f56904

Please sign in to comment.