如何配置 socket.io 1.3.7 首先使用 websocket
how to configure socket.io 1.3.7 to use websocket first
升级 socket.io 到 1.3.7 之后
io.set("transports", ["websocket", "xhr-polling"]);
不再有效,我应该在哪里配置这些选项?
(我在官方文档中找不到这个信息)
你试过这个吗:
var socket = require('socket.io')({
transports : ["websocket", "xhr-polling"]
});
您可以像这样配置传输首选项:
// server
var io = require('socket.io')({
transports : [ 'websocket', 'xhr-polling' ]
});
默认情况下,socket.io@1
将尝试升级到可用的网络套接字,但最初,客户端将始终尝试使用 XHR/JSONP 进行连接,并在可用时升级到网络套接字。
这种行为可能会令人困惑(我第一次使用它时让我感到困惑),因为它看起来像是 XHR 轮询。已记录 here:
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 之后
io.set("transports", ["websocket", "xhr-polling"]);
不再有效,我应该在哪里配置这些选项? (我在官方文档中找不到这个信息)
你试过这个吗:
var socket = require('socket.io')({
transports : ["websocket", "xhr-polling"]
});
您可以像这样配置传输首选项:
// server
var io = require('socket.io')({
transports : [ 'websocket', 'xhr-polling' ]
});
默认情况下,socket.io@1
将尝试升级到可用的网络套接字,但最初,客户端将始终尝试使用 XHR/JSONP 进行连接,并在可用时升级到网络套接字。
这种行为可能会令人困惑(我第一次使用它时让我感到困惑),因为它看起来像是 XHR 轮询。已记录 here:
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.