英文:
Proper way to connect to Socket.IO server using Golang
问题
我正在学习Go,并尝试使用这个库来在Go中实现一个socket.io客户端。
我正在尝试从一个Go编写的客户端连接到一个Node.js Socket.IO服务器。我一直在连接到Node.js服务器时遇到一些问题,连接总是失败并显示"websocket: bad handshake",服务器端没有任何输出,网络上提供的答案都没有给我一个可行的解决方案。
我觉得我可能找到了问题的原因。根据我在socket.io的NPM文档中所读到的:
注意:Socket.IO不是一个WebSocket实现。尽管Socket.IO在可能的情况下确实使用WebSocket作为传输方式,但它在每个数据包中添加了一些元数据:数据包类型、命名空间和当需要消息确认时的ack id。这就是为什么WebSocket客户端无法成功连接到Socket.IO服务器,而Socket.IO客户端也无法连接到WebSocket服务器(如ws://echo.websocket.org)。
然而,我使用的socket.io客户端库(名为"go-socketio")只允许我使用"ws"或"wss"协议连接到socket.io服务器,因此它不起作用,我无法连接到我的Node.js socket.io服务器。
**是否有一种正确的方法可以从基于Go的客户端连接到Node.js Socket.IO服务器?**我阅读的所有文章都只展示了一个基于Go的socket.io服务器,没有客户端。我尝试了多个客户端库,但它们都不起作用(或者它们没有足够的文档来展示如何将库用作客户端连接到服务器):(
(只要服务器端可以使用Node.js编写,并允许同时使用Express.js作为服务器的其余部分,我愿意使用Go可能更好支持的双向通信库)
提前感谢您的帮助,请告诉我您可能需要的任何进一步信息来回答这个问题。
(我目前不在家,因此无法上传代码片段,但我回家后会上传)
英文:
I am learning Go and am trying to use this library to implement a socket.io client in Go.
I am trying to connect from a client (written in Go) to a Node.js Socket.IO server. I have been having some issues with connecting to the Node.js server, the connection will always fail with "websocket: bad handshake" with no output on the server-side and none of the answers available on the web have given me a working solution yet.
I feel like I may have found the cause for my issue. From what I have read on the NPM Docs for socket.io:
> Note: Socket.IO is not a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server (like ws://echo.websocket.org) either.
However, the socket.io client library I use (which is called go-socketio
) only allows me to connect to a socket.io server using the ws
or wss
protocol, therefore it doesn't work and I cannot connect to my Node.js socket.io server.
Is there a proper way to connect to a Node.js Socket.IO server from a Go-based client? All of the articles I have read documenting this only show a Go-based socket.io server, not a client. I have tried multiple client libraries and none of them work (or they do not have enough documentation showing how to use the library as a client connecting to a server)
(I would be willing to use another bi-directional communication library which Go may have better support for as long as the serverside can be written in Node.js and allows the usage of Express.js alongside it as the rest of the server is written in Node.js and uses Express.js)
Thanks in advance and please inform me of any further information you may need to answer this question.
(I am currently not at home and thus cannot upload snippets of code, but I will do so once I get home)
答案1
得分: 1
抱歉给你带来不便!我已经找到了一个解决方案。通过使用ws包替代socket.io包作为我的服务器,我成功让Go客户端工作了。
谢谢!
英文:
Sorry for the inconvenience!
I have found a solution. By using the ws package instead of the socket.io package for my server, I have got the Go client to work.
Thanks
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论