Golang插座IO无法建立WebSocket连接

发布于 2025-02-07 06:49:14 字数 1967 浏览 0 评论 0 原文

这是我的插座IO GO服务器,基本上是按照 package 的示例代码:

func main() {
    server := socketio.NewServer(nil)
    server.OnConnect("/", func(s socketio.Conn) error {
        s.SetContext("")
        fmt.Println("connected:", s.ID())       
        return nil
    })

    server.OnEvent("/", "notice", func(s socketio.Conn, msg string) {
        fmt.Println("notice:", msg)
        s.Emit("reply", "have "+msg)
    })

    server.OnEvent("/chat", "msg", func(s socketio.Conn, msg string) string {
        s.SetContext(msg)
        return "recv " + msg
    })

    server.OnEvent("/", "bye", func(s socketio.Conn) string {
        last := s.Context().(string)
        s.Emit("bye", last)
        s.Close()
        return last
    })

    server.OnError("/", func(s socketio.Conn, e error) {
        fmt.Println("meet error:", e)
    })

    server.OnDisconnect("/", func(s socketio.Conn, reason string) {
        fmt.Println("closed", reason)
    })

    go server.Serve()
    defer server.Close()

    mux := http.NewServeMux()
    
    mux.Handle("/socket.io/", server) // socket io
    
    log.Println("Serving at localhost:8000...")
    log.Fatal(http.ListenAndServe(":8000", cors.Default().Handler(mux)))
}

和这是我的客户端代码:

<script src="https://cdn.socket.io/4.5.0/socket.io.min.js"
  integrity="sha384-7EyYLQZgWBi67fBtVxw60/OWl1kjsfrPFcaU0pp0nAh+i8FD068QogUvg85Ewy1k" crossorigin="anonymous"></script>
<script>
  const socket = io("http://localhost:8000");
  socket.emit("msg", "abc")
</script>

我的客户端只需继续发送长插图获取请求,无法建立Websocket连接。我怀疑这可能与我的服务器响应有关吗?从浏览器DevTool中,似乎我在响应有效载荷前面有一些随机字节:

“在此处输入图像描述”

我无法解决问题。任何帮助将不胜感激,谢谢!

this is my socket io Go server, basically following the package's example code:

func main() {
    server := socketio.NewServer(nil)
    server.OnConnect("/", func(s socketio.Conn) error {
        s.SetContext("")
        fmt.Println("connected:", s.ID())       
        return nil
    })

    server.OnEvent("/", "notice", func(s socketio.Conn, msg string) {
        fmt.Println("notice:", msg)
        s.Emit("reply", "have "+msg)
    })

    server.OnEvent("/chat", "msg", func(s socketio.Conn, msg string) string {
        s.SetContext(msg)
        return "recv " + msg
    })

    server.OnEvent("/", "bye", func(s socketio.Conn) string {
        last := s.Context().(string)
        s.Emit("bye", last)
        s.Close()
        return last
    })

    server.OnError("/", func(s socketio.Conn, e error) {
        fmt.Println("meet error:", e)
    })

    server.OnDisconnect("/", func(s socketio.Conn, reason string) {
        fmt.Println("closed", reason)
    })

    go server.Serve()
    defer server.Close()

    mux := http.NewServeMux()
    
    mux.Handle("/socket.io/", server) // socket io
    
    log.Println("Serving at localhost:8000...")
    log.Fatal(http.ListenAndServe(":8000", cors.Default().Handler(mux)))
}

And this is my client side code:

<script src="https://cdn.socket.io/4.5.0/socket.io.min.js"
  integrity="sha384-7EyYLQZgWBi67fBtVxw60/OWl1kjsfrPFcaU0pp0nAh+i8FD068QogUvg85Ewy1k" crossorigin="anonymous"></script>
<script>
  const socket = io("http://localhost:8000");
  socket.emit("msg", "abc")
</script>

My client side just keep sending long-polling GET requests, the websocket connection cannot be established. I suspect it might have something to do with my server response? From the browser devtool, it seems like I have some random bytes in front of my response payload:

enter image description here

I wasn't able to solve the issue. Any help would be appreciated, thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2025-02-14 06:49:14

您正在使用客户端的4.5.0版本以及(我假设 - 请) github.com/googollee/go-socket.io go package package readme 状态:

当前该库支持socket.io客户端的1.4版本。它支持房间,名称空间和广播。

socket io docs 提供兼容性图表,显示服务器的V1是仅由客户端版本1支持。因此以下似乎有效( playground> playground - 注意,我也做出了细微的更改):

<script src="https://cdn.socket.io/socket.io-1.7.4.min.js"></script>
<script>
  const socket = io("http://localhost:8000");
  socket.emit("msg", "abc");
</script>

要使用后期版本的socket.io,您将需要一个支持该协议后期版本的服务器。 gosf 似乎支持V2,但我没有尝试过。

You are using version 4.5.0 of the client along with (I assume - minimal reproducable examples please) github.com/googollee/go-socket.io. The go package readme states:

Current this library supports 1.4 version of the Socket.IO client. It supports room, namespaces and broadcast at now.

The socket io docs provide a compatibility chart showing that v1 of the server is only supported by version 1 of the client. So the following appears to work (playground - note I also made subtle changes so the message is output):

<script src="https://cdn.socket.io/socket.io-1.7.4.min.js"></script>
<script>
  const socket = io("http://localhost:8000");
  socket.emit("msg", "abc");
</script>

To use a later version of Socket.IO you will need a server that supports a later version of the protocol. gosf appears to support v2 but I have not tried it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文