socket.io 握手返回错误“传输未知" [英] socket.io handshake return error "Transport unknown"

查看:17
本文介绍了socket.io 握手返回错误“传输未知"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 elephant.io 将事件从我的 PHP 脚本发送到我的节点服务器.使用这个库执行测试时,我注意到握手没有按预期发生.

I'm trying to use elephant.io to send event from my PHP script to my nodejs server. Performing tests with this library I've noticed that the handshake was not happening as expected.

阅读有关使用 socket.io 的客户端-服务器握手的规范后,我已经对我的 nodejs 服务器测试了一个简单的握手请求:

After reading the specs about client-server handshake with socket.io, I've tested a simple handshake request to my nodejs server:

POST "http://acme.local:3700/socket.io/1"

但这会返回以下 JSON 消息:

But this is returning the following JSON message:

{
    "code": 0,
    "message": "Transport unknown"
}

我不确定这个错误是由于我使用的 socket.io 版本 (v1.0.2) 还是握手请求格式不正确.

I'm not sure if this error is due to the version of socket.io I'm using (v1.0.2) or if the handshake request is simply malformed.

推荐答案

问题是 socket.io v1.0.x 改进了它的传输层.在 0.9.x 中,socket.io 首先尝试建立 websocket 连接,如果 websocket 被阻塞、超时等,则回退到轮询.因此您可以从schema://addr:port/socket.io/1/.

The problem is socket.io v1.0.x revamped its transport layer. In 0.9.x, socket.io tries to establish a websocket connection firstly and fall back to polling if websocket is blocked, timeout, etc. So you can download a websocket configuration string like "sid:interval_time:timeout_time:..." from schema://addr:port/socket.io/1/.

但是,在 v.1.0.x 中,客户端必须先建立一个广泛支持的轮询传输,然后升级到 websocket.传输配置通过 json 格式化:schema://addr:port/socket.io/?transport=polling.json 对象看起来像 {"sid":"xxx", "upgrade":["websocket",..],"pingInterval":xxx,"pingTimeout":xxx}.

However, in v.1.0.x, the client has to establish a polling transportation first, which is widely supported, then upgrade to websocket. And the transport configuration is formatted in json via: schema://addr:port/socket.io/?transport=polling. The json object looks like {"sid":"xxx", "upgrade":["websocket",..],"pingInterval":xxx,"pingTimeout":xxx}.

因此任何适用于 v0.9.x 的客户端都无法与 v1.0.x 通信.大象.io 有一个临时修复:https://github.com/Wisembly/elephant.io/pull/53/files#diff-8f2bc4c2d1b3889bc393b67f296edbc5R97.但是我无法让它工作.

So any client works for v0.9.x can't communicate with v1.0.x. There is a temporary fix for elephant.io: https://github.com/Wisembly/elephant.io/pull/53/files#diff-8f2bc4c2d1b3889bc393b67f296edbc5R97 . However I can't get it to work.

据推测,现在 socket.io v1.0.x 的所有 3rd 方客户端都是其 JavaScript 代码的完整端口.我试图根据上面的elephant.io帖子修复socketio4net,但失败了.

Presumably all 3rd-party clients for socket.io v1.0.x nowadays are full-port of its JavaScript code. I tried to fix socketio4net according to the elephant.io post above but failed.

08/26 更新.

这是我得到的:

如何与socket.io v1.0服务器通信:

How to communicate with socket.io v1.0 server:

  1. 获取 http[s]://host:port/socket.io/?transport=polling

  1. GET http[s]://host:port/socket.io/?transport=polling

服务器在响应正文中响应一个 JSON 配置字符串,其中包含一些未知字符作为标头.

Server responds a JSON config string in response body with some unknown characters as header.

警告 c 风格的 char* 用户:此字符串以 '' 开头.

Warning for c-style char* useres: this string begins with ''.

字符串看起来像:xxxx {"sid":"xxx", "upgrades":["websocket","polling",..], pingInterval:xxxx, pingTimeout:xxxx}.

The string looks like: xxxx {"sid":"xxx", "upgrades":["websocket","polling",..], pingInterval:xxxx, pingTimeout:xxxx}.

sid:websocket 连接的会话 ID.

sid: seesion id for websocket connection.

升级:可用的运输方式.请确保websocket"是其中之一.

upgrades: available transport method. Please make sure "websocket" is one of them.

pingInterval &pingTimeout:每隔 pingInterval ping 服务器并在 pingTimeout 内检查 pong.

pingInterval & pingTimeout: ping server every pingInterval and check for pong within pingTimeout.

在 ws[s]://host:port/socket.io/?transport=websocket&sid=sid

Establish websocket connection at ws[s]://host:port/socket.io/?transport=websocket&sid=sid

连接成功后向socket.io服务器发送字符串52".

Send string "52" to socket.io server upon successful connection.

监听服务器消息,等待字符串40"确认客户端和服务器之间的websocket链接.

Listen on server message, waiting for string "40" to confirm websocket link between client and server.

发送您在服务器上设置的任何其他命令.

Send any other commands you setup on sever.

注意:

v1.0 将其消息传输格式更改为 engine.io_type + socket.io_type + real_message_in_json.(我不知道 v0.9 中的端点在哪里.)

v1.0 changed its message transport format to engine.io_type + socket.io_type + real_message_in_json. (I don't know where endpoint in v0.9 kicks in.)

engine.io 消息类型:

engine.io message type:

  • 打开 =0
  • 关闭 =1
  • ping =2
  • 乒乓=3
  • 消息 =4
  • 升级=5
  • noop =6

socket.io 消息类型:

socket.io message type:

  • 连接 = 0
  • 断开连接 = 1
  • 事件 = 2
  • 确认 = 3
  • 错误 = 4
  • binary_event = 5
  • binary_ack = 6

因此,52"表示 UPGRADE_EVENT,40"表示 MESSAGE_CONNECT.通常,服务器消息以42"开头,即 MESSAGE_EVENT.PING 和 PONG 不需要 socket.io 消息.我想知道 UPGRADE 是否也这样.

So, "52" means UPGRADE_EVENT and "40" is MESSAGE_CONNECT. Typically, server messages start with "42", which is MESSAGE_EVENT. PING and PONG don't require a socket.io message. I wonder if UPGRADE works like that too.

获得一个可用的 websocket 客户端,你就可以开始了!

Get a working websocket client and you are good to go!

附言大象.io 3.0正在开发中,支持v1.0.

P.S. elephant.io 3.0 is on the way, which supports v1.0.

这篇关于socket.io 握手返回错误“传输未知"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆