go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

when i'm useing websocket, there are error : "write response failed, error: http: connection has been hijacked"

Open Tony-Jack2017 opened this issue 1 year ago • 4 comments

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:

  1. 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)
     	    }
         }
     }
    
    1. 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

Tony-Jack2017 avatar Jul 08 '22 03:07 Tony-Jack2017

Please give a runnable example, thanks!

kevwan avatar Jul 08 '22 14:07 kevwan

ok, there is a example of my project: https://github.com/Tony-Jack2017/work-test.git

Tony-Jack2017 avatar Jul 09 '22 06:07 Tony-Jack2017

https://github.com/zeromicro/zero-examples/tree/main/chat

Please try this to see if anything wrong.

kevwan avatar Jul 09 '22 07:07 kevwan

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Aug 09 '22 02:08 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Nov 08 '22 02:11 github-actions[bot]