Response does not implement http.Hijacker

前端 未结 2 1456
长发绾君心
长发绾君心 2020-12-21 16:55

I\'m using Go and trying to implement WebSocket in my project. while implementing this. I get "WebSocket: response does not implement HTTP.Hijacker" error. I\'m ne

2条回答
  •  礼貌的吻别
    2020-12-21 17:12

    To cite the docs on http.Hijacker:

    ~$ go doc http.Hijacker

    package http // import "net/http"
    
    type Hijacker interface {
      // Hijack lets the caller take over the connection.
      // After a call to Hijack the HTTP server library
      // will not do anything else with the connection.
      //
      // It becomes the caller's responsibility to manage
      // and close the connection.
      //
      // The returned net.Conn may have read or write deadlines
      // already set, depending on the configuration of the
      // Server. It is the caller's responsibility to set
      // or clear those deadlines as needed.
      //
      // The returned bufio.Reader may contain unprocessed buffered
      // data from the client.
      //
      // After a call to Hijack, the original Request.Body must not
      // be used. The original Request's Context remains valid and
      // is not canceled until the Request's ServeHTTP method
      // returns.
      Hijack() (net.Conn, *bufio.ReadWriter, error)
    }
    

    The Hijacker interface is implemented by ResponseWriters that allow an HTTP handler to take over the connection.

    The default ResponseWriter for HTTP/1.x connections supports Hijacker, but HTTP/2 connections intentionally do not. ResponseWriter wrappers may also not support Hijacker. Handlers should always test for this ability at runtime.

    So, I see several possibilities for your problem to happen:

    • You're trying to turn an HTTP/2 connection to a Websocket.
    • You're using some "middleware" which wraps whatever object the stock net/http package passes to your handler as net/http.ResponseWriter with something which does not bother to implement the proper Hijack method to support the net/http.Hijacker interface.

提交回复
热议问题