静态html页面直接创建了WebSocket连接golang服务器

时间:2013-04-08 16:40:44

标签: sockets websocket go

我正在撰写需要为服务器创建html的{​​{1}}页面

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

然而,在Chrome26中,响应是

WebSocket与' ws://127.0.0.1:1234 /'的连接失败:意外的响应代码:400

是否遗漏了某些东西(如握手)?

这是我的websocket,服务器正在使用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>

1 个答案:

答案 0 :(得分:5)

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

要解决此问题,您只需要从go - 服务器上提供服务器。

因此,请将您的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.go文件相同的目录中:

<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中进行连接。