이동 중에도 websocket을 사용합니다. 아래
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 : 그리고는이 같은 구글 사소한 예에서 웹 소켓 URL 형식을 가지고
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)
}
}
사실 나는 그것을 설명 할 수없는 반면 정규 표현식이라고 생각했습니다.
나는 내 자신의 PC 브라우저에서 테스트하고 성공을 연결합니다
http://localhost:8080
하지만
http://ip:8080 (which ip is my computer's also the litsening server's ip)
없습니다. 그리고 왜?
물론 "ws : // {{$}}/ws"를 특정 URL로 변경하면 작동합니다. 그러나 나는 왜 그런지 알고 싶습니다. 그리고이 표현은 무엇을 위해 일치 할 수 있습니까?
완전한 예제 코드가 크고, 위의 질문에 충분하다고 생각합니다. 내가 뭔가를 놓친다면이 페이지에서 완전한 예제를 찾을 수 있습니다 : https://github.com/garyburd/go-websocket/tree/master/examples/chat
전체 예제를 게시하십시오. http : // localhost : port working 및 http : // ip : port는 올바른 ip : port를 사용하지 않고 방화벽이 설정되어 있고 localhost에 바인딩하고 있다는 것을 의미 할 수 있습니다. –
@StephenWeinberg 귀하의 생각을 알려 주셔서 감사합니다. 이미 게시물을 수정하고 변경했습니다. 희망은 지금 당신에게 분명합니다. – xsuii