Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"res.(http.Flusher).Flush()" not work #4

Open
xiongsongsong opened this issue Aug 6, 2021 · 1 comment
Open

"res.(http.Flusher).Flush()" not work #4

xiongsongsong opened this issue Aug 6, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@xiongsongsong
Copy link

xiongsongsong commented Aug 6, 2021

when use golang direct run,it's perfect:
https://user-images.githubusercontent.com/342509/128476357-3f6f97de-7f09-4b16-97b7-39571450390c.mov

package main

import (
	"fmt"
	"net/http"
	"time"
)

func main() {
http.HandleFunc("/wawawa",  func(res http.ResponseWriter, req *http.Request) {
	flusher := res.(http.Flusher)
	res.Header().Set("Content-Type", "text/html")
	fmt.Fprintf(res, "<!doctype html>\n<html><head><title>test</title></head><body>")
	for i := 1; i <= 50; i++ {
		fmt.Fprintf(res, "<p>Chunk #%d</p>\n<script>console.log(new Date())</script>\n", i)
		// perfect
		flusher.Flush()
		time.Sleep(500 * time.Millisecond)
	}
	fmt.Fprintf(res, "</body>\n</html>")
})

	http.ListenAndServe(":8080", nil)

	select {}
}

but with wasm, it's not work:
https://user-images.githubusercontent.com/342509/128476360-f9d80484-c6e4-4bb8-805d-5fc4273727e2.mov

package main

import (
	"fmt"
	"net/http"
	"time"
	wasmhttp "github.com/nlepage/go-wasm-http-server"
)

func main() {
	http.HandleFunc("/wawawa",  func(res http.ResponseWriter, req *http.Request) {
		flusher := res.(http.Flusher)
		res.Header().Set("Content-Type", "text/html")
		fmt.Fprintf(res, "<!doctype html>\n<html><head><title>test</title></head><body>")
		for i := 1; i <= 50; i++ {
			fmt.Fprintf(res, "<p>Chunk #%d</p>\n<script>console.log(new Date())</script>\n", i)
			// not work, 25 s later
			flusher.Flush()
			time.Sleep(500 * time.Millisecond)
		}
		fmt.Fprintf(res, "</body>\n</html>")
	})

	wasmhttp.Serve(nil)

	select {}
}
@xiongsongsong xiongsongsong added the enhancement New feature or request label Aug 6, 2021
@nlepage
Copy link
Owner

nlepage commented Aug 7, 2021

Hi @xiongsongsong
Yes this is an expected behavior.
go-wasm-http-server first records the entire response then sends it in one piece to the browser.
So in your case the first 25 seconds are spent recording the response, then all the html content is sent in a whole chunk to the browser.
For now this kind of response "streaming" isn't possible.
There is a related issue #2, about using a ReadableStream for the response body, which should allow this kind of streaming.
I'm going to try using ReadableStream, I'll keep you updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants