go-zero
go-zero copied to clipboard
when i'm useing websocket, there are error : "write response failed, error: http: connection has been hijacked"
My problem In msg gateway service , after the websocket conn is builded, i send the message, there sender is recevied response, and recipient also receive the messgage. But the second send message is failed There are error :"httpx/responses.go:78","content":"write response failed, error: http: connection has been hijacked"
To Reproduce Steps to reproduce the behavior, if applicable:
-
The code is
func ws() { var wsConfig wsconfig.Config conf.MustLoad(*wsConfigFile, &wsConfig) serveCtx := servectx.NewServiceContext(wsConfig) wsConfig.RestConf.Timeout = 0 server := rest.MustNewServer(wsConfig.RestConf) defer server.Stop() handler.RegisterHandler(server, serveCtx) utils.PrintlnSuccess("Starting websocket server at") server.Start() }
func RegisterHandler(server *rest.Server, serveCtx *servectx.ServiceContext) { logic.NewMsgGatewayLogic(context.Background(), serveCtx) server.AddRoutes([]rest.Route{ { Method: http.MethodGet, Path: "/login", Handler: MsgGatewayHandler(serveCtx), }, }) }
func MsgGatewayHandler(serveCtx *servectx.ServiceContext) http.HandlerFunc { return func(writer http.ResponseWriter, request *http.Request) { var req types.Request if err := httpx.Parse(request, &req); err != nil { httpx.Error(writer, err) return } ws := logic.NewMsgGatewayLogic(context.Background(), serveCtx) // verify token resp, ok := ws.MsgGatewayVerify(&req) if ok { logx.Info("resp uid is", resp.Uid) err := ws.WsUpgrade(resp.Uid, writer, request, nil) if err != nil { return } httpx.WriteJson(writer, http.StatusOK, resp) } else { http.Error(writer, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) } } }
- The error is
2022/07/08 10:44:34 http: response.WriteHeader on hijacked connection from github.com/zeromicro/go-zero/rest/handler. (*loggedResponseWriter).WriteHeader (loghandler.go:66)
2022/07/08 10:44:34 http: response.Write on hijacked connection from github.com/zeromicro/go-zero/rest/handler. (*loggedResponseWriter).Write (loghandler.go:62)
{"@timestamp":"2022-07-08T10:44:34.266+08:00","caller":"httpx/responses.go:78","content":"write response failed, error: http: connection has been hijacked","level":"error"}
Environments :
- OS: window
- go-zero version 1.3.4
- goctl version 1.3.8
Please give a runnable example, thanks!
ok, there is a example of my project: https://github.com/Tony-Jack2017/work-test.git。
https://github.com/zeromicro/zero-examples/tree/main/chat
Please try this to see if anything wrong.
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.