“xhr-polling”配置在socket.io中做了什么?

时间:2020-12-14 23:13:02

I have a node.js server with 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);
  ...
}

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和10秒的轮询持续时间,这是否意味着每10秒钟会调用一个新连接?如果是这样,如果用户断开连接,我该如何跟踪用户?我在heroku上运行node.js.

2 个解决方案

#1


15  

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

xhr-polling意味着您的服务器将在收到的任何GET POST时等待10秒,它在回答之前没有答案,而不是发回空响应。因此,如果您的服务器在10秒后没有信息要回馈,它将以空响应回复。你可以在这里阅读更多:长轮询

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.

我个人使用xhr-polling作为来自nodejitsu上的应用程序中的WebSockets的后备选项(另一个像Heroku一样托管的节点)并且它工作正常。唯一的问题是“on connection”事件需要大约3-8秒而不是像我的WebSocket应用程序那样是即时的。

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.

没有在每次新轮询上创建新连接,这只是每10秒只向服务器发送一次GET或POST的方式,而不是每隔0.5秒轮询一次服务器以获得“真实 - 时间“申请。如果服务器在不到10秒的时间内应答,则会发送另一个轮询以准备下一个答案。

I hope this will help you. Have a good day.

我希望这能帮到您。祝你有美好的一天。

#2


3  

socket.io reconnects clients automatically.

socket.io自动重新连接客户端。

#1


15  

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

xhr-polling意味着您的服务器将在收到的任何GET POST时等待10秒,它在回答之前没有答案,而不是发回空响应。因此,如果您的服务器在10秒后没有信息要回馈,它将以空响应回复。你可以在这里阅读更多:长轮询

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.

我个人使用xhr-polling作为来自nodejitsu上的应用程序中的WebSockets的后备选项(另一个像Heroku一样托管的节点)并且它工作正常。唯一的问题是“on connection”事件需要大约3-8秒而不是像我的WebSocket应用程序那样是即时的。

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.

没有在每次新轮询上创建新连接,这只是每10秒只向服务器发送一次GET或POST的方式,而不是每隔0.5秒轮询一次服务器以获得“真实 - 时间“申请。如果服务器在不到10秒的时间内应答,则会发送另一个轮询以准备下一个答案。

I hope this will help you. Have a good day.

我希望这能帮到您。祝你有美好的一天。

#2


3  

socket.io reconnects clients automatically.

socket.io自动重新连接客户端。