lxzan / gws

simple, fast, reliable websocket server & client, supports running over tcp/kcp/unix domain socket. keywords: ws, proxy, chat, go, golang...
https://pkg.go.dev/github.com/lxzan/gws
Apache License 2.0
1.43k stars 93 forks source link

升级socket时报错hijack #28

Closed wbbbbbbbbbbbbbbbbbb closed 1 year ago

wbbbbbbbbbbbbbbbbbb commented 1 year ago

我在gozero项目中接入gws,按照chatroom的例子来写的时候,报错以下错误: error write response failed, error: http: connection has been hijacked caller=httpx/responses.go:81

lxzan commented 1 year ago

贴段代码看看

lxzan commented 1 year ago

随便写了个demo, 没发现有问题

var upgrader = gws.NewUpgrader(&Handler{}, nil)

func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
    server.AddRoutes(
        []rest.Route{
            {
                Method:  http.MethodGet,
                Path:    "/connect",
                Handler: WsHandler(serverCtx),
            },
        },
    )
}

func WsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {

    return func(w http.ResponseWriter, r *http.Request) {
        conn, err := upgrader.Upgrade(w, r)
        if err != nil {
            log.Println(err.Error())
            return
        }
        conn.ReadLoop()
    }
}

type Handler struct {
    gws.BuiltinEventHandler
}
wbbbbbbbbbbbbbbbbbb commented 1 year ago

sry。。是我的问题,我设置过期时间的时候忘记 * time.Second 导致关闭的过快了。