【问题标题】:Static html page created the WebSocket connection golang server directly静态html页面直接创建WebSocket连接golang服务器
【发布时间】:2013-04-08 16:40:44
【问题描述】:

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

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

然而,在 Chrome26 中,响应是

WebSocket 连接到“ws://127.0.0.1:1234/”失败:意外响应代码: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)
    }
}

【问题讨论】:

    标签: sockets websocket go


    【解决方案1】:

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

    要解决这个问题,您只需从 go-server 提供您的 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.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 中进行连接了。

    【讨论】:

    • 但我不想自己处理协议,golang 没有处理这个吗?
    • 这并不是“处理协议”,而是更多地处理浏览器。我已经更新了您的代码以添加文件处理程序。如果您从文件系统加载文件,您将无法连接,您必须通过 http 加载文件,最好从同一来源。
    • 其实我已经测试过其他语言让服务器像java netty,c#他们都支持接受html创建websocket,在服务器中没有,它只是一个html,我认为有一个标签在像“原始”这样的协议中是错过的,因为 html 是本地的,而 golang websocket 只是拒绝这个
    • 我发现有人自己处理协议使这成为可能github.com/garyburd/go-websocket/tree/master/websocket
    • 我想补充一点,Go 网络服务器不接受在其请求中没有来源的套接字。使用一些 JS 从资源管理器中打开一个 html 文件会导致网络服务器返回一个意外的结果:403
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    相关资源
    最近更新 更多