此websocket网址“ ws:// {{$}} / ws”是什么意思?

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:

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

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

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's also the litsening server'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

doupo2157
doupo2157 感谢您的提醒。我已经重新编辑,并且对我的帖子进行了一些更改。希望您现在明白了。
接近 7 年之前 回复
dongyao4419
dongyao4419 请发布您的完整示例。运行localhost:port和ip:port可能意味着您未使用正确的ip:port,已防火墙,绑定到localhost等。
接近 7 年之前 回复

1个回答



我猜您正在使用 template 包。 模板包支持 {{占位符}} </ code>,并在这些大括号中加以注释。 这些大括号可能包含诸如 range </ code>, if </ code>等的语句以及变量名。 变量名 $ </ code>是一个特殊名称,它指向传递给 template.Execute </ code>方法的根元素。</ p>

请 添加您的 wsServe </ code>方法的代码,以便我们可以看到您传递给模板的值。 </ p>
</ div>

展开原文

原文

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.

duangao8359
duangao8359 您的回答很正确,对我的粗心表示歉意。 我添加了您提到的代码,还提供了指向完整示例的链接。 并感谢您的回答。
接近 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问