如何配置socket.io 1.3.7先使用websocket [英] how to configure socket.io 1.3.7 to use websocket first

查看:20
本文介绍了如何配置socket.io 1.3.7先使用websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级socket.io到1.3.7后

after upgrading socket.io to 1.3.7

io.set("transports", ["websocket", "xhr-polling"]);

不再工作了,我在哪里配置这些选项?(我在官方文档中找不到此信息)

doesn't work anymore, where do I configure those options? (I can't find this information in official docs)

推荐答案

您可以像这样配置传输首选项:

You can configure transport preferences like this:

// server
var io = require('socket.io')({
  transports : [ 'websocket', 'xhr-polling' ]
});

默认情况下,如果可用,socket.io@1 将尝试升级到 Web 套接字,但最初,客户端将始终尝试使用 XHR/JSONP 连接,并升级到 Web 套接字如果有的话.

By default, socket.io@1 will try to upgrade to web sockets if they are available, but initially, the client will always try to connect using XHR/JSONP, and upgrade to web sockets if available.

这种行为可能令人困惑(我第一次使用它时让我感到困惑),因为它看起来像是 XHR 轮询.它记录在此处:

This behaviour can be confusing (it confused me the first time I used it), because it looks like it's XHR-polling. It's documented here:

Socket.IO 永远不会假设 WebSocket 会正常工作,因为在实践中很有可能不会.相反,它会立即与 XHR 或 JSONP 建立连接,然后尝试将连接升级到 WebSocket.与依赖超时的回退方法相比,这意味着您的任何用户的体验都不会下降.

Socket.IO never assumes that WebSocket will just work, because in practice there’s a good chance that it won’t. Instead, it establishes a connection with XHR or JSONP right away, and then attempts to upgrade the connection to WebSocket. Compared to the fallback method which relies on timeouts, this means that none of your users will have a degraded experience.

这篇关于如何配置socket.io 1.3.7先使用websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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