Hi,
I've also implemented proxy trough hijacking. I've used the exact webSocket http.Handler you provided in your example. However I'm serving my proxy trough path "/" thus I have to distinguish between HTTP requests and incoming websocket connection in order to call the webSocket http.Handler. My ServeHTTP is like:
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if isWebsocket(r) {
p := websocketProxy("backendTargetURL") //example URL
p.ServeHTTP(w, r)
return
}
if h := s.handler(r); h != nil {
h.ServeHTTP(w, r)
return
}
http.Error(w, "Not found.", http.StatusNotFound)
}
To check for Websocket, I'm using the function:
func isWebsocket(req *http.Request) bool {
conn_hdr := ""
conn_hdrs := req.Header["Connection"]
if len(conn_hdrs) > 0 {
conn_hdr = conn_hdrs[0]
}
upgrade_websocket := false
if strings.ToLower(conn_hdr) == "upgrade" {
upgrade_hdrs := req.Header["Upgrade"]
if len(upgrade_hdrs) > 0 {
upgrade_websocket = (strings.ToLower(upgrade_hdrs[0]) == "websocket")
}
}
return upgrade_websocket
}
If this returns true, I'm calling my webSocket http.ProxyHandler. Is this a correct approach? Or is it better when I listen to a path like:
http.Handle("/ws", websocketProxy)
And route all websocket connections to this handler and call this websocket handler directly? (without using the isWebsocket function above)
Which way is better and is the implementation correct?
Thanks in advance
Regards