socket.io 是不是将传输从轮询升级到 websocket?

Posted

技术标签:

【中文标题】socket.io 是不是将传输从轮询升级到 websocket?【英文标题】:Does socket.io upgrade transport to websocket from polling ?socket.io 是否将传输从轮询升级到 websocket? 【发布时间】:2014-12-23 20:30:31 【问题描述】:

我正在使用带有 socket.io 的 nodejs 和 express 框架。以下是我运行命令时得到的日志输出

DEBUG=* /bin/www

以下是日志输出。

  engine intercepting request for path "/socket.io/" +2m
  engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=1414498393419-0" +0ms
  engine handshaking client "WECN1dP0tobacKc8AAAB" +1ms
  engine:socket sending packet "open" ("sid":"WECN1dP0tobacKc8AAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000) +0ms
  engine:polling setting request +0ms
  engine:socket flushing buffer to transport +0ms
  engine:polling writing "  �0"sid":"WECN1dP0tobacKc8AAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000" +1ms
  engine:socket executing batch send callback +1ms
  socket.io:server incoming connection with id WECN1dP0tobacKc8AAAB +2.5m
  socket.io:client connecting to namespace / +1.3m
  socket.io:namespace adding socket to nsp / +2.5m
  socket.io:socket socket connected - writing packet +1.3m
  socket.io:socket joining room WECN1dP0tobacKc8AAAB +0ms
  socket.io:client writing packet "type":0,"nsp":"/" +1ms
  socket.io-parser encoding packet "type":0,"nsp":"/" +2.5m
  socket.io-parser encoded "type":0,"nsp":"/" as 0 +0ms
  engine:socket sending packet "message" (0) +1ms
  socket.io:socket joined room WECN1dP0tobacKc8AAAB +5ms
  engine intercepting request for path "/socket.io/" +140ms
  engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=1414498393557-1&sid=WECN1dP0tobacKc8AAAB" +0ms
  engine setting new request for existing client +1ms
  engine:polling setting request +0ms
  engine:socket flushing buffer to transport +0ms
  engine:polling writing "�40" +0ms
  engine:socket executing batch send callback +0ms
  engine upgrading existing transport +39ms
  engine:socket might upgrade socket transport from "polling" to "websocket" +0ms
  engine intercepting request for path "/socket.io/" +16ms
  engine handling "GET" http request "/socket.io/?EIO=3&transport=polling&t=1414498393618-2&sid=WECN1dP0tobacKc8AAAB" +0ms
  engine setting new request for existing client +0ms
  engine:polling setting request +0ms
  engine:ws received "2probe" +2ms
  engine:ws writing "3probe" +0ms
  engine:socket writing a noop packet to polling for fast upgrade +100ms
  engine:polling writing "�6" +0ms
  engine:ws received "5" +4ms
  engine:socket got upgrade packet - upgrading +0ms
  engine:ws received "2" +25s
  engine:socket packet +0ms
  engine:socket got ping +0ms
  engine:socket sending packet "pong" (undefined) +0ms
  engine:socket flushing buffer to transport +0ms
  engine:ws writing "3" +0ms
  engine:ws received "2" +25s
  engine:socket packet +0ms
  engine:socket got ping +0ms
  engine:socket sending packet "pong" (undefined) +0ms
  engine:socket flushing buffer to transport +0ms
  engine:ws writing "3" +0ms
  engine:ws received "2" +25s
  engine:socket packet +0ms
  engine:socket got ping +0ms
  engine:socket sending packet "pong" (undefined) +0ms
  engine:socket flushing buffer to transport +0ms
  engine:ws writing "3" +0ms
  engine:ws received "2" +25s
  engine:socket packet +0ms
  engine:socket got ping +0ms
  engine:socket sending packet "pong" (undefined) +0ms
  engine:socket flushing buffer to transport +0ms
  engine:ws writing "3" +1ms

我没有看到任何确认传输升级的日志消息。乒乓消息也不断发生。

这是否意味着 websocket 传输已建立?

【问题讨论】:

您在节点应用程序后面使用反向代理吗? 没有。代码直接命中 localhost:3000 【参考方案1】:

是的,它使用polling first and then upgrades to websocket。我确定如果您使用浏览器连接或使用 wireshark,您会看到来自服务器的“HTTP/1.1 101 Switching Protocols”消息。 您还可以在日志中看到“engine:ws”,这意味着 websocket 正在工作。 如果您仍然不确定,您还可以打印出传输类型(例如断开连接)。这是服务器端代码(socket.io 1.1.0):

  socket.on('disconnect', function () 
    console.log(socket.id + ' disconnect ('+ socket.client.conn.transport.constructor.name +')';
  );

【讨论】:

嗨。如果您看到日志Bug engine:ws writing "3probe" +0msengine:socket writing a noop packet to polling for fast upgrade +100ms 之前。即甚至在尝试从轮询升级到 websocket 之前。这意味着engine:ws的出现并不意味着传输已经转移到websocket 是的,确实如此,但这并不意味着运输总体上发生了变化。但在日志的末尾,它记录得很好。无论如何,我只想尝试套接字传输类型的调试输出。我在我的服务器中使用它,当客户端连接它时,它正在轮询,我们断开它的 websocket。

以上是关于socket.io 是不是将传输从轮询升级到 websocket?的主要内容,如果未能解决你的问题,请参考以下文章

在 android 上使用 Socket.IO 总是返回 XHR 轮询错误

Socket.io 没有流式传输到多个连接

为啥我不断收到 Socket.io 轮询请求的 404 错误?

接收错误:xhr 轮询错误 socket.io 客户端反应

socket.io 通过 XHR 轮询强制断开连接

连接错误:io.socket.engineio.client.EngineIOException:xhr 轮询错误