Replies: 2 comments
-
解决了吗? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Have you tried it with some other client? When I use this example and curl - it seems that it works func main() {
e := echo.New()
e.Use(middleware.Logger())
e.GET("/stream", func(c echo.Context) error {
i := 0
for {
select {
case <-c.Request().Context().Done(): // case request canceled!!
fmt.Println("---------closing----------- ") //this doesnt get fired when the
return nil // user leaves the page or close tab
default:
}
c.Response().Write([]byte(fmt.Sprintf("%d\n", i)))
c.Response().Flush()
time.Sleep(250 * time.Millisecond)
i++
}
})
if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
} Output from command line x@x:~/code/$ curl http://localhost:8080/stream -N
0
1
2
3
4
5
6
7
8
9
10
11
^C output from server:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Im using Pocketbase, which uses echo v5. So my problem is that c.Request().Context().Done() is not signaling/working when the frontend/client closes the browser window/tab or refreshes the page. I'm streaming contents of a txt file line bye line with a delay between every written line. It works fine, but I want to stop/cancel the streaming of the txt file when the user leaves or closes the page.
Here the go code:
and the js code with svelte, where i just trigger the fn with a button:
If something isn't clear, just ask.
I appreciate any help!
Beta Was this translation helpful? Give feedback.
All reactions