【问题标题】:Http Redirect in Web Socket Connection [now better explained]Web Socket 连接中的 Http 重定向 [现在更好地解释]
【发布时间】:2013-01-05 00:33:45
【问题描述】:

我认为我最初的问题(见栏下方)过于含糊。我编了以下傻 举例说明我的观点。

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)
}

此代码不起作用,因为我无法重复使用 w 来执行重定向。 那么,在这种情况下是否可以重定向,例如通过另一个 http包调用?

我希望我已经说清楚了......

提前致谢

克里斯


我正在以下配置中的“我的服务器”上工作

[web browser client] -- [my server] -- [foreign webservice]

“我的服务器”向“网络浏览器客户端”提供以下内容

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)
    }
}

RootHandler 将 html 页面写入“Web 浏览器客户端”,而 socketHandler 在“网络浏览器客户端”和 “我的服务器”。

来自“网络浏览器客户端”的请求通过网络套接字连接到达,并且 由“我的服务器”处理,“我的服务器”与“外部网络服务”通信 来确定响应。

通常,“外国网络服务”会提供一些数据,供以下用户使用 “我的服务器”响应“网络浏览器客户端”。

在某些情况下,“外部网络服务”会以完整的 html 页面进行响应。

我不确定如何将此类 html 页面传达给“网络浏览器客户端”

到目前为止,我最好的猜测是执行 http.Redirect() 到 launchHandler 这样 即launchHandler可以编写从“外国Web服务器”获得的html页面 到“网络浏览器客户端”。 (web socket连接关闭时没有问题。)

为此,我使用 http.ResponseWriter, *http.Request 作为参数 到首先在 rootHandler 中接收到的 httpRedirect。

这不起作用并产生日志消息:“http:multiple response.WriteHeader calls”

关于如何解决此问题的任何建议?也许我执行 http.Redirect 的方法 是错的;也许我应该获得另一个 http.ResponseWriter 和/或 *http.Request 价值观。

感谢任何帮助!

【问题讨论】:

  • 鉴于到目前为止没有响应,您可能需要提供导致此问题的实际代码。我承认,根据您的描述,我对您想要实现的目标有些困惑。

标签: go


【解决方案1】:

您需要根据您给出的新示例进行一些更改:

  • 一旦您调用 fmt.Fprint,您就会返回一个响应并因此返回一个标头(状态代码为 200)。检查http://golang.org/pkg/net/http/#ResponseWriter

    // Changing the header after a call to WriteHeader (or Write) has no effect.

如果您想要一个简单的重定向,请忽略 fmt.Fprint(w, "hello, this is root")

  • /redir/redir/ 是不同的端点。您需要更改处理程序的 URL 或重定向。
  • 如果你想显示一个页面,然后在后台下载后重定向,我建议使用带有第三个处理程序的 Javascript,你可以从根处理程序返回的 HTML 页面在后台调用它,并有一个完成后重定向用户的回调

例如..:

   +------------+ $.GET +---------------------+   
   | Root page  | ----> |  Background handler |  
   |            |       +---------+-----------+
   |            |                 |
   +------------+ <---Task done---+
         |
         v [redirect using JS]
    +---------------+  
    |/redir/ handler|
    +---------------+

另一种选择可能是使用 iframe 来显示“外国网页”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-21
    • 2020-06-12
    • 2020-05-10
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    相关资源
    最近更新 更多