英文:
Golang socket io unable to establish websocket connection
问题
这是我的Socket.IO Go服务器,基本上遵循了该包的示例代码:
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>
我的客户端只是不断发送长轮询的GET请求,无法建立WebSocket连接。我怀疑这可能与我的服务器响应有关。从浏览器开发工具来看,似乎在响应有效负载之前有一些随机字节。
我无法解决这个问题。任何帮助将不胜感激,谢谢!
英文:
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:
I wasn't able to solve the issue. Any help would be appreciated, thank you!
答案1
得分: 1
您正在使用客户端的4.5.0版本,以及(我假设-请提供最小可重现示例)github.com/googollee/go-socket.io
。go package readme中提到:
当前该库支持Socket.IO客户端的1.4版本。目前支持房间、命名空间和广播。
socket io文档提供了一个兼容性表格,显示v1服务器仅由客户端的v1版本支持。因此,以下代码似乎可以工作(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论