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
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 byResponseWriters
that allow an HTTP handler to take over the connection.The default
ResponseWriter
for HTTP/1.x connections supportsHijacker
, but HTTP/2 connections intentionally do not.ResponseWriter
wrappers may also not supportHijacker
. Handlers should always test for this ability at runtime.
So, I see several possibilities for your problem to happen:
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.