静态html页面直接在服务器上创建WebSocket连接



我正在写一个html页面,需要创建一个websocket到服务器

在服务器上,我使用了"code.google.com/p/go.net/websocket"中的示例,只是接受连接。

然而,在Chrome26中响应是

WebSocket连接到'ws://127.0.0.1:1234/' failed: Unexpected response code: 400

是不是错过了什么(比如握手)?

这是我的html和服务器使用go

<html>
<head></head>
<body>
<script type="text/javascript">
    var sock = null;
    var wsuri = "ws://127.0.0.1:1234";
    window.onload = function() {
        console.log("onload");
        sock = new WebSocket(wsuri);
        sock.onopen = function() {
            console.log("connected to " + wsuri);
        }
        sock.onclose = function(e) {
            console.log("connection closed (" + e.code + ")");
        }
        sock.onmessage = function(e) {
            console.log("message received: " + e.data);
        }
    };
    function send() {
        var msg = document.getElementById('message').value;
        sock.send(msg);
    };
</script>
<h1>WebSocket Echo Test</h1>
<form>
    <p>
        Message: <input id="message" type="text" value="Hello, world!">
    </p>
</form>
<button onclick="send();">Send Message</button>
</body>
</html>

//------------------------------

package main
import (
    "code.google.com/p/go.net/websocket"
    "fmt"
    "log"
    "net/http"
)
func Echo(ws *websocket.Conn) {
    var err error
    for {
        var reply string
        if err = websocket.Message.Receive(ws, &reply); err != nil {
            fmt.Println("Can't receive")
            break
        }
        fmt.Println("Received back from client: " + reply)
        msg := "Received:  " + reply
        fmt.Println("Sending to client: " + msg)
        if err = websocket.Message.Send(ws, msg); err != nil {
            fmt.Println("Can't send")
            break
        }
    }
}
func main() {
    http.Handle("/", websocket.Handler(Echo))
    if err := http.ListenAndServe(":1234", nil); err != nil {
        log.Fatal("ListenAndServe:", err)
    }
}

Chrome可能会抛出错误400,因为它认为您正在尝试对websocket服务器进行跨域请求,并且认为您不太可能获得许可。

要解决这个问题,你只需要从你的go服务器服务器你的html。

sock.go代码改为:

package main
import (
    "code.google.com/p/go.net/websocket"
    "fmt"
    "log"
    "net/http"
)
func Echo(ws *websocket.Conn) {
    var err error
    for {
        var reply string
        if err = websocket.Message.Receive(ws, &reply); err != nil {
            fmt.Println("Can't receive")
            break
        }
        fmt.Println("Received back from client: " + reply)
        msg := "Received:  " + reply
        fmt.Println("Sending to client: " + msg)
        if err = websocket.Message.Send(ws, msg); err != nil {
            fmt.Println("Can't send")
            break
        }
    }
}
func main() {
    http.Handle("/", http.FileServer(http.Dir("."))) // <-- note this line
    http.Handle("/socket", websocket.Handler(Echo))
    log.Println("serving")
    if err := http.ListenAndServe(":1234", nil); err != nil {
        log.Fatal("ListenAndServe:", err)
    }
}

并将index.html文件添加到与sock相同的目录中。文件:

<html>
<head></head>
<body>
<script type="text/javascript">
    var sock = null;
    var wsuri = "ws://127.0.0.1:1234/socket"; // <-- note new path
    window.onload = function() {
        console.log("onload");
        sock = new WebSocket(wsuri);
        sock.onopen = function() {
            console.log("connected to " + wsuri);
        }
        sock.onclose = function(e) {
            console.log("connection closed (" + e.code + ")");
        }
        sock.onmessage = function(e) {
            console.log("message received: " + e.data);
        }
    };
    function send() {
        var msg = document.getElementById('message').value;
        sock.send(msg);
    };
</script>
<h1>WebSocket Echo Test</h1>
<form>
    <p>
        Message: <input id="message" type="text" value="Hello, world!">
    </p>
</form>
<button onclick="send();">Send Message</button>
</body>
</html>

现在你将能够从chrome内连接。

相关内容

  • 没有找到相关文章

最新更新