什么是"xhr-polling"?配置在socket.io吗? [英] What does "xhr-polling" config do in socket.io?

查看:729
本文介绍了什么是"xhr-polling"?配置在socket.io吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有socket.io的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);
  ...
}

使用xhr-polling和10秒的轮询持续时间,这是否意味着每10秒将调用一个新连接?如果是这样,如果用户不断断开连接,我该如何跟踪他们?我在heroku上运行node.js.

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.

推荐答案

xhr-polling意味着您的服务器将在收到任何POST的GET消息之前等待10秒,该GET消息在回答之前没有答案,而不是发送回去.空的回应.因此,如果您的服务器在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

我个人在nodejitsu(另一个像Heroku这样的托管节点)上的应用程序中使用xhr-polling作为WebSockets的后备选项,并且运行良好.唯一的事情是连接时"事件,该事件大约需要3-8秒,而不是像我的WebSocket应用程序那样即时发生.

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.

在每次新的轮询中都不会创建新的连接,这只是每10秒仅向服务器发送一次GET或POST的一种方式,而不必每隔0.5秒对服务器进行一次轮询就可以了. 实时"应用程序.如果服务器在10秒以内答复,则将发送另一个轮询以准备下一个答案.

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.

这篇关于什么是"xhr-polling"?配置在socket.io吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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