Golang:自动刷新HTTP页面

huangapple go评论78阅读模式
英文:

Golang: Automatic Refresh of a HTTP Page

问题

我正在使用Go编程语言执行一个HTTP页面。GO中的函数如下所示:

func main(){
    ...
    http.HandleFunc("/Page", func(w http.ResponseWriter, r *http.Request) {
        t:=template.New("New template")
        child_template := t.New("New child template")
        _, _ = child_template.Parse(output)  // output是省略代码中的内容
        t, err = t.ParseFiles("HTML_template.html")
        _ = t.ExecuteTemplate(w, "HTML_template.html", output)
    }
}

如何使/Page自动刷新?我尝试了以下方法,但没有成功。

func main(){
    ...
    http.HandleFunc("/Page", func(w http.ResponseWriter, r *http.Request) {
        for{
            t:=template.New("New template")
            child_template := t.New("New child template")
            _, _ = child_template.Parse(output)  // output是省略代码中的内容
            t, err = t.ParseFiles("HTML_template.html")
            _ = t.ExecuteTemplate(w, "HTML_template.html", output)

            time.Sleep(time.Millisecond*100)
        }
    }
}

我正在尝试制作一个动态图表,用于绘制每秒钟传入数据的数量。如果我不断刷新浏览器,坐标轴也会重新加载,看起来很丑。HTML_template.html的内容如下:

<script type="text/javascript">
    function plot(){
        ...
        var data = [{{template "New child template"}}];
        ...
    }
    setInterval(func(){plot()},500);
</script>
英文:

I have a HTTP page getting executed using Go programming language. The function in GO looks like this:

func main(){
    ...
    http.HandleFunc(&quot;/Page&quot;, func(w http.ResponseWriter, r *http.Request) {
        t:=template.New(&quot;New template&quot;)
        child_template := t.New(&quot;New child template&quot;)
        _, _ = child_template.Parse(output)  // output is from the omitted code
        t, err = t.ParseFiles(&quot;HTML_template.html&quot;)
        _ = t.ExecuteTemplate(w, &quot;HTML_template.html&quot;, output)
    }
}

How do I make /Page refreshes by itself? I have tried the following, but it doesn't work.

func main(){
    ...
    http.HandleFunc(&quot;/Page&quot;, func(w http.ResponseWriter, r *http.Request) {
        for{
            t:=template.New(&quot;New template&quot;)
            child_template := t.New(&quot;New child template&quot;)
            _, _ = child_template.Parse(output)  // output is from the omitted code
            t, err = t.ParseFiles(&quot;HTML_template.html&quot;)
            _ = t.ExecuteTemplate(w, &quot;HTML_template.html&quot;, output)

            time.Sleep(time.Millisecond*100)
        }
    }
}

I am trying to make a dynamic graph that plots the number of incoming data per second. If I keep making the browser refreshes, the axis will get reloaded too and it looks ugly. The HTML_template.html looks like this

&lt;script type = &quot;text/javascript&quot;&gt;
    function plot(){
        ...
        var data = [{{template &quot;New child template&quot;}}];
        ...
    }
    setInterval(func(){plot()},500);
&lt;/script&gt;

答案1

得分: 6

这可以通过简单地添加refresh元标签来实现,而无需使用Go、JavaScript、AJAX、SSE或Websockets。

将以下代码添加到页面的<head>标签中:

<meta http-equiv="refresh" content="3" />

这将使页面每3秒刷新一次。

英文:

This can be done without Go, JavaScript, AJAX, SSE or Websockets by simply adding the refresh meta tag. Adding

&lt;meta http-equiv=&quot;refresh&quot; content=&quot;3&quot; /&gt;

into your page's &lt;head&gt; will cause it to refresh every 3 seconds.

答案2

得分: 1

你正在以错误的方式进行操作,你应该使用Websockets Gorilla 或者 go.net,或者至少使用ajax,但是重新加载整个页面非常低效。

英文:

You're going at this the wrong way, you should use Websockets Gorilla's or go.net's or at the very least use ajax, but reloading the whole page is very inefficient.

答案3

得分: -1

你真的真的真的需要使用WebSockets来完成这个任务,但是有一个快速而简单的方法。

在页面中添加一点jQuery代码:

if (window.location.href.indexOf('reload')==-1) {
     window.location.replace(window.location.href+'?reload');
}

但是你真的需要使用WebSockets。

英文:

Your really really really need to use websockets to do this but there is a quick and dirty method.

Add a little jquery to the page:

if (window.location.href.indexOf(&#39;reload&#39;)==-1) {
     window.location.replace(window.location.href+&#39;?reload&#39;);
}

But you really need to use websockets.

huangapple
  • 本文由 发表于 2014年7月4日 01:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/24560111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定