7
我的问题 在消息网关服务中,在建立 websocket 连接后,我发送了消息,发送者收到了响应,接收者也收到了消息。但第二次发送消息失败,出现错误:“httpx/responses.go:78”,“content”:“写入响应失败,错误:http:连接已被劫持”
重现 步骤以重现该行为(如果适用):
-
代码如下
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) } } }
- 错误是
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"}
- 错误是
环境:
- 操作系统:窗口
- go-zero 版本 1.3.4
- goctl 版本 1.3.8