这个websocket的URL “ws://{{$}}/ws” 是什么意思?

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

What does this websocket url "ws://{{$}}/ws" mean?

问题

我在Go语言中使用WebSocket。我从一个简单的示例中找到了一个WebSocket URL的格式,如下所示:

ws://{{$}}/ws

以下是相对完整的代码:

home.html:

<html>
<head>
<title>Chat Example</title>
<script type="text/javascript">
    $(function() {
        ......
        if (window["WebSocket"]) {
            conn = new WebSocket("ws://{{$}}/ws");
            conn.onclose = function(evt) {
                appendLog($("<div><b>Connection closed.</b></div>"))
            }
            conn.onmessage = function(evt) {
                appendLog($("<div/>").text(evt.data))
            }
        } else {
            appendLog($("<div><b>Your browser does not support WebSockets.</b></div>"))
        }
        ......
    });
</script>
</head>
</html>

而wsServer.go:

package main

import (
    "flag"
    "log"
    "net/http"
    "text/template"
)
var addr = flag.String("addr", ":8080", "http service address")
var homeTempl = template.Must(template.ParseFiles("home.html"))

func serveHome(w http.ResponseWriter, r *http.Request) {
    ......
    w.Header().Set("Content-Type", "text/html; charset=utf-8")
    homeTempl.Execute(w, r.Host)
}

func main() {
    http.HandleFunc("/", serveHome)
    http.HandleFunc("/ws", serveWs)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

我以为这是一个正则表达式,但实际上我无法解释它。

我在我的个人电脑浏览器上进行了测试,并成功连接到:

http://localhost:8080

但是

http://ip:8080 (其中ip是我的计算机的IP地址,也是监听服务器的IP地址)

却无法连接。为什么?

当然,当我将"ws://{{$}}/ws"更改为特定的URL时,它可以工作。但我想知道为什么?这个表达式可以匹配什么?

完整的示例代码很长,我认为上面的内容已经足够回答问题了。如果我漏掉了什么,你可以在这个页面上找到完整的示例:https://github.com/garyburd/go-websocket/tree/master/examples/chat

英文:

I work with websocket in go. And I got a websocket url format from a trivial example that I google like this:

ws://{{$}}/ws

Relatively complete code below:

home.html:

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Chat Example&lt;/title&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
    $(function() {
        ......
        if (window[&quot;WebSocket&quot;]) {
            conn = new WebSocket(&quot;ws://{{$}}/ws&quot;);
            conn.onclose = function(evt) {
                appendLog($(&quot;&lt;div&gt;&lt;b&gt;Connection closed.&lt;/b&gt;&lt;/div&gt;&quot;))
            }
            conn.onmessage = function(evt) {
                appendLog($(&quot;&lt;div/&gt;&quot;).text(evt.data))
            }
        } else {
            appendLog($(&quot;&lt;div&gt;&lt;b&gt;Your browser does not support WebSockets.&lt;/b&gt;&lt;/div&gt;&quot;))
        }
        ......
    });
&lt;/script&gt;
&lt;/head&gt;
&lt;/html&gt;

And wsServer.go:

package main

import (
    &quot;flag&quot;
    &quot;log&quot;
    &quot;net/http&quot;
    &quot;text/template&quot;
)
var addr = flag.String(&quot;addr&quot;, &quot;:8080&quot;, &quot;http service address&quot;)
var homeTempl = template.Must(template.ParseFiles(&quot;home.html&quot;))

func serveHome(w http.ResponseWriter, r *http.Request) {
    ......
    w.Header().Set(&quot;Content-Type&quot;, &quot;text/html; charset=utf-8&quot;)
    homeTempl.Execute(w, r.Host)
}

func main() {
    http.HandleFunc(&quot;/&quot;, serveHome)
    http.HandleFunc(&quot;/ws&quot;, serveWs)
    err := http.ListenAndServe(:8080, nil)
    if err != nil {
        log.Fatal(&quot;ListenAndServe: &quot;, err)
    }
}

I thought it would be a regular expression while actually I can't explain it.

I test it on my own PC browser, and connect success with:

http://localhost:8080 

but

http://ip:8080 (which ip is my computer&#39;s also the litsening server&#39;s ip)

not.
And why?

Of course it works when I change "ws://{{$}}/ws" to a certain url. But I want to know why?And what can this expression matching for?

The complete example code is large, I think above is enough to the question. If I miss something you can find out complete example in this page : https://github.com/garyburd/go-websocket/tree/master/examples/chat

答案1

得分: 2

我猜你正在使用Go的template包。模板包支持用花括号注释的{{占位符}}。这些花括号中可能包含rangeif等语句以及变量名。变量名$是一个特殊的名称,指向传递给template.Execute方法的根元素。

请添加你的wsServe方法的代码,这样我们可以看到你将什么值传递给你的模板。我会在之后扩展我的回答。

英文:

I'm guessing you are using the template package of Go. The template package supports {{ placeholders }} that are annotated by those curly brackets. Those curly brackets might contain statements like range, if etc, and variable names. The variable name $ is a special name that points to the root element that was passed to the template.Execute method.

Please add the code of your wsServe method so that we can see what value your are passing to your template. I will extend my answer afterwards.

huangapple
  • 本文由 发表于 2013年8月18日 00:14:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/18290860.html
匿名

发表评论

匿名网友

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

确定