我有一个使用流行的
如果我要在我的服务器上使用socket.io,连接代码将如下所示:
但这会导致以下结果:
ws
包来使用WebSockets的Node.js服务器。我想使用此库连接到运行socket.io的第三方服务器。如果我要在我的服务器上使用socket.io,连接代码将如下所示:
const socket = socketIo('https://api.example.com/1.0/scores')
我尝试使用ws
包连接相同的服务,并修改了URL:
const wsClient = new WebSocket('wss://api.example.com/1.0/scores');
但这会导致以下结果:
问题: 如何连接到运行socket.io的第三方服务器,从运行错误:意外的服务器响应:200
ws
包的服务器?
附加信息:
- I've noticed in my searches that some people have suggested appending
/socket.io/?EIO=3&transport=websocket
to the end of the url. This does not throw the same error as above (> Error: Unexpected server response: 200
) nor throw any visible error, but does not appear to work (no data is received from the remote server). Using
new WebSocket('ws://api.example.com/1.0/scores?EIO=3&transport=websocket');
to open the connection (viaws
) results in the following stack trace:{ Error: Parse Error at Socket.socketOnData at emitOne at Socket.emit // ... }
NODE_DEBUG=net,https,socket*,ws* node server.js
运行您的服务器代码,并在连接发生时发布日志。此问题主要与第三方服务器未正确处理握手有关。 - Tarun Lalwani'ws://api.example.com/1.0/scores'
,则尝试ws://api.example.com/1.0/scores?EIO=3&transport=websocket
。 - bgran