Response does not implement http.Hijacker

前端 未结 2 1472
长发绾君心
长发绾君心 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:31

    The application is using "middleware" that wraps the net/http server's ResponseWriter implementation. The middleware wrapper does not implement the Hijacker interface.

    There are two fixes for the problem:

    • Remove the offending middleware.

    • Implement the Hijacker interface on the middleware wrapper by delegating through to the underlying response. The method implementation will look something like this:

      func (w *wrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
          h, ok := w.underlyingResponseWriter.(http.Hijacker)
          if !ok {
              return nil, nil, errors.New("hijack not supported")
          }
          return h.Hijack()
      }
      

    If you don't know what the response writer wrapper is, add a statement to print the type from the handler:

    func HandleConnections(w http.ResponseWriter, r *http.Request) {
        fmt.Printf("w's type is %T\n", w)
        ...
    

提交回复
热议问题