Golang的socket io无法建立WebSocket连接。

huangapple go评论88阅读模式
英文:

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(&quot;/&quot;, func(s socketio.Conn) error {
		s.SetContext(&quot;&quot;)
		fmt.Println(&quot;connected:&quot;, s.ID())		
		return nil
	})

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

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

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

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

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

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

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

And this is my client side code:

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

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:

Golang的socket io无法建立WebSocket连接。

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.iogo 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):

&lt;script src=&quot;https://cdn.socket.io/socket.io-1.7.4.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
  const socket = io(&quot;http://localhost:8000&quot;);
  socket.emit(&quot;msg&quot;, &quot;abc&quot;);
&lt;/script&gt;

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.

huangapple
  • 本文由 发表于 2022年6月14日 22:46:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/72619023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定