philippseith / signalr

SignalR server and client in go
MIT License
131 stars 39 forks source link

Client failed to establish a connection: not a io.ReadWriteCloser: *http.cancelTimerBody #196

Closed ryebrand closed 5 months ago

ryebrand commented 5 months ago

When I try to establish a connection with the server using the following code on the client side, I receive this error. Can you help me out?

failed to WebSocket dial: response body is not a io.ReadWriteCloser: *http.cancelTimerBody


// 处理从服务器接收的消息
type receiver struct{}

// ReceiveOrderStatus 回调方法,用于处理服务器的"ReceiveOrderStatus"调用
func (r *receiver) ReceiveOrderStatus(message string) {
    fmt.Println("New Message:", message)
}
headers := http.Header{}
    headers.Set("UserID", UserID)
    token, err := GetTokenFromRedis(context.Background(), UserID)
    if err != nil {
        fmt.Println("get token from redis error: ", err)
        return
    }
    headers.Set("Token", token)
    client, err = signalr.NewClient(context.Background(), nil,
        signalr.WithReceiver(&receiver{}),
        signalr.WithConnector(func() (signalr.Connection, error) {
            creationCtx, _ := context.WithTimeout(context.Background(), 5*time.Minute)
            //defer cancel()
            return signalr.NewHTTPConnection(creationCtx, address, signalr.WithHTTPHeaders(func() http.Header {
                return headers
            }))
        }),
        signalr.Logger(kitlog.NewLogfmtLogger(os.Stdout), true),
    )

    if err != nil {
        fmt.Println("Error creating SignalR client:", err)
        return
    }
    // 启动signalR 客户端
    client.Start()
    fmt.Println("Client started!!!")

image

ryebrand commented 5 months ago

i find this code will be error image

philippseith commented 5 months ago

This happens in nhooyr.io/websocket with the http.DefaultClient. Did you change http.DefaultClient? I do not think this is a problem caused somewhere in our repo.