Note that there are some explanatory texts on larger screens.

plurals
  1. POHttp Redirect in Web Socket Connection [now better explained]
    primarykey
    data
    text
    <p>I think my original question (see below the bar) was too vague. I have made up the following silly example that shows what is my point.</p> <pre><code>package main import ( "fmt" "net/http" "time" ) func main() { http.HandleFunc("/redir", redirHandler) http.HandleFunc("/", rootHandler) _ = http.ListenAndServe("localhost:4000", nil) } func redirHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "hello, this is redir which will host the foreign html page") } func rootHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "hello, this is root") // at some point "foreign webserver" sends me a complete // html page. // I want to serve this page at "/redir/", and I feel my // best shot is to redirect to "/redir" time.Sleep(2 * time.Second) http.Redirect(w, r, "/redir/", http.StatusFound) } </code></pre> <p>This code does not work, as I cannot re-use w to perform the re-direct. So, is it possible to redirect in this case, for example by means of another http package call?</p> <p>I hope I've made myself clear...</p> <p>Thanks in advance</p> <p>Chris</p> <hr> <p>I am working on "my server" in the following configuration</p> <pre><code>[web browser client] -- [my server] -- [foreign webservice] </code></pre> <p>"my server" provides the following to "web browser client"</p> <pre><code>func main() { http.HandleFunc("/launch", launchHandler) http.HandleFunc("/", rootHandler) http.Handle("/socket", websocket.Handler(socketHandler)) err := http.ListenAndServe(listenAddr, nil) if err != nil { log.Fatal(err) } } </code></pre> <p>RootHandler writes an html page to "web browser client", and socketHandler establishes a websocket connection between "web browser client" and "my server".</p> <p>Requests from "web browser client" arrive over the web socket connection and are handled by "my server", "my server" communicates with "foreign webservice" to determine the response.</p> <p>Usually, "foreign webservice" gives a little bit of data which is used by "my server" to respond to "web browser client".</p> <p>In some occasions, "foreign webservice" responds with a complete html page.</p> <p>I am not sure how to communicate such html page to "web browser client"</p> <p>My best guess so far is to execute a http.Redirect() to launchHandler such that launchHandler can write the html page as obtained from "foreign web server" to "web browser client". (It is no problem when the web socket connection is closed.)</p> <p>In order to do so, I use the http.ResponseWriter, *http.Request as parameters to httpRedirect which were received in rootHandler in the first place.</p> <p>This does not work and produced log message: "http: multiple response.WriteHeader calls"</p> <p>Any suggestions on how to fix this? Maybe my approach to perform a http.Redirect is wrong; Maybe I should obtain an other http.ResponseWriter and/or *http.Request values.</p> <p>Any help is appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload