-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
How to use SSE Response with go-zero? #3967
Comments
me too |
Respond with HTTP header |
I have achieved it in handler function like: return func(w http.ResponseWriter, r *http.Request) {
_, ok := w.(http.Flusher)
w.Header().Set(`Content-Type`, `text/event-stream;charset=utf-8`) and return streaming data with channel in golang. |
For example, Django in Python, it provides some response objects such as StreamingHttpResponse. It makes much elegant to achieve SSE. I hope go-zero can refer to similar programming. |
me too |
func LoginQrCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.LoginQrCodeReq
w.Header().Set(`Content-Type`, `text/event-stream;charset=utf-8`)
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := user.NewLoginQrCodeLogic(r.Context(), svcCtx, w)
_, err := l.LoginQrCode(&req, w)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
httpx.Ok(w)
}
func (l *LoginQrCodeLogic) LoginQrCode(req *types.LoginQrCodeReq, w http.ResponseWriter) (resp string, err error) {
// todo: add your logic here and delete this line
fmt.Fprintf(w, "data: %s\n\n", fmt.Sprintf("Event %s", "3333"))
w.(http.Flusher).Flush()
return
} The above code implements sse, but I need another interface. When sending data, I don't know how to call it |
这是自动回复邮件。
您好,您的邮件已到达我的邮箱,我将尽快查阅并回复,谢谢。Hello, I have reveived your email. But due to the time differences and my busy work, perhaps I will reply to you in 24 to 48 hours.
|
请问实现了吗?.api文件和NewLoginQrCodeLogic是如何写的? |
对的,想问下api文件怎么写,有无具体的example |
Yes, I want to ask how to write the API file and whether there are specific example |
My application requires SSE Response(text/event-stream), but I cannot find the documentation related to SSE. How should I use SSE to return with go-zero?Thanks.
The text was updated successfully, but these errors were encountered: