-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
[HELP WANTED] How to use serverpush with Gin? #1200
Comments
ctx.Writer is not a simple http.ResponseWriter, so I modify the ResponseWriter interface , add Pusher() function to get http.Pusher type ResponseWriter interface {
http.ResponseWriter
http.Hijacker
http.Flusher
http.CloseNotifier
// Returns the HTTP response status code of the current request.
Status() int
// Returns the number of bytes already written into the response http body.
// See Written()
Size() int
// Writes the string into the response body.
WriteString(string) (int, error)
// Returns true if the response body was already written.
Written() bool
// Forces to write the http header (status code + headers).
WriteHeaderNow()
// return http.Pusher if support http2 push
Pusher() http.Pusher
}
func (w *responseWriter) Pusher() http.Pusher {
if pusher, ok := w.ResponseWriter.(http.ResponseWriter).(http.Pusher); ok {
return pusher
}
return nil
}
// in the api
router.GET("/pusher", func(c *gin.Context) {
if pusher := c.Writer.Pusher(); pusher != nil {
// use pusher.Push() to push
}
} |
@htobenothing , could you pls provide a demo case, I use your solution locally, but it seems the push is not working. the file I pushed will be "GET" again by my index.html |
See #1273 PR |
Please update the latest code. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to implement Server Push.
I've taken the code from this blog post: https://blog.golang.org/h2push
Where ctx - *gin.Context
It returns "push not ok". Original example works in my browser.
Any ideas? Or any way to get original http.ResponseWriter from gin.Context?
Of course, there is https and browser shows h2 protocol.
The text was updated successfully, but these errors were encountered: