“xhr-polling”是什么意思?在socket.io中配置吗?
我有一个带有 socket.io 的 node.js 服务器:
var io = require('socket.io').listen(app);
// assuming io is the Socket.IO server object
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
io.sockets.on('connection', function(socket){
console.log('connected: %s', socket.id);
...
}
使用 xhr-polling 且轮询持续时间为 10 秒,这是否意味着每 10 秒就会调用一个新连接?如果是这样,如果用户不断断开连接,我如何跟踪他们?我在heroku 上运行node.js。
I have a node.js server with socket.io:
var io = require('socket.io').listen(app);
// assuming io is the Socket.IO server object
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
io.sockets.on('connection', function(socket){
console.log('connected: %s', socket.id);
...
}
With xhr-polling and a polling duration of 10 seconds, does this mean that a new connection will be invoked every 10 seconds? If so, how can I keep track of users if they keep disconnecting? I'm running node.js on heroku.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
xhr-polling 意味着您的服务器将在收到的任何 GET 或 POST 上等待 10 秒,如果没有答案,则在回答之前而不是发回空响应。因此,如果您的服务器在 10 秒后没有信息可返回,它将返回一个空响应。
您可以在这里阅读更多信息:长轮询
我个人使用 xhr-polling 作为后备选项Nodejitsu(另一个像 Heroku 一样托管的节点)上的应用程序中的 WebSockets 运行良好。唯一的问题是“连接时”事件大约需要 3-8 秒,而不是像我的 WebSocket 应用程序那样是即时的。
每次新轮询时不会创建新连接,这只是每 10 秒仅向服务器发送一次 GET 或 POST 的一种方式,而不必每 0.5 秒轮询一次服务器以获得“真实的连接”。时间”应用程序。如果服务器在 10 秒内回答,将发送另一个轮询以准备下一个回答。
我希望这会对您有所帮助。
祝你有美好的一天。
xhr-polling means that your server will be waiting for 10 seconds on any GET of POST received that it does not have an answer before answering instead of sending back an empty response. So, if your server has no information to give back after 10 seconds, it will answer back with an empty response.
You can read more here : Long polling
I personally use xhr-polling as a fallback option from WebSockets in an application on nodejitsu (another node hosting like Heroku) and it is working fine. The only thing is the "on connection" event that takes about 3-8 seconds instead of being instant as with my WebSocket application.
There is no new connection that is created on every new polling, it is just a way that only one GET or POST is sent to the server every 10 seconds, instead of having to poll the server every .5 seconds to have a "real-time" application. If the server answer in less than 10 seconds, there will be another poll sent to prepare the next answer.
I hope this will help you.
Have a good day.
socket.io
自动重新连接客户端。socket.io
reconnects clients automatically.