如何同步2个或更多异步请求的数据并使用解析的数据发送响应? [英] How to sync data of 2 or more asynchronous requests and send responses with the data parsed?

查看:213
本文介绍了如何同步2个或更多异步请求的数据并使用解析的数据发送响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过最简单的例子,让我们说他们是2个或更多需要通过发出HTTP请求来提交数据的人,其中一个人提交的数据必须等待其他人发送的其他数据到服务器以便解析。这是我想在Node.js中完成的图像:

By taking the simplest example is let's say that they are 2 or more dudes that need to submit data by making an HTTP request, the data submitted by one of the dudes has to wait for the other data to be sent by other dudes to server in order to be parsed. Here is an image of what I am trying to accomplish in Node.js:

解析数据时,需要将其作为对每个人的回复发回。问题是我坚持实现这个任务(我使用http模块,长轮询,后来我将使用websocket),问题在于回调本质,我尝试使用脏方法,如制作一个数组填充,直到每个人都提交并在setInterval函数的帮助下检查,无论何时解析数据等等。

When the data is parsed it needs to be sent back as responses to each of the dudes. The problem is that I am stuck on implementing this task(I am using http module, and long polling, later I will use the websocket), the problem resides in the callbacks nature, and I tried using dirty methods like making an array that is filled until everyone submitted and check with the help of setInterval function wherever it's time to parse data and so on.

是否有更清洁,更简洁的方法来实现这些任务?

Is there any cleaner and less messy ways to implement such tasks?

推荐答案

我会给这个过程一个唯一的id(两个客户都知道),然后将相关数据存储在数据库中或直接在服务器RAM中。有待处理的请求(请求 - 等待)对服务器来说是不好的,因为它必须保持越来越多的开放连接,并且对于客户端来说是不好的,因为他看到旋转的圈子而不知道发生了什么。我宁愿做以下事情:

I would give a unique id to this process ( which both clients know), then store the related data in a database or directly in the servers RAM. Having pending requests (request - wait) is bad for the server as it has to keep more and more open connections, and bad for the client as he sees a spinning circle without knowing whats happening. I would rather do the following:

1. client sends request with data
      2. data gets stored, a *please wait* is returned
3. the page reloads all two seconds
      4. if all the datas there, return the data
      5. if not, return *please wait* and goto 3

伪实现看起来像这个:

var processes = new Map();

function Process (){
  do {
   var id = Math floor(Math.random()*10**10);
  }while(processes.has(id));
  this.id = id;
  processes.set(id,this);
}

Process.prototype.pending = function(res){
 if(this.first && this.second) 
   return res.end(this.first + this.second);

 res.end(`
  <html>
   <body>
    Please wait... 
    <script> setTimeout(location.reload.bind(location),2000);
    </script>
   </body>
 </html>`);
};

//the routing
var app = Express();

app.get("/create",function(req,res){
 var p = new Process();
 req.end(p.id);
});

app.get("/first/:id/:data",function(req,res){
 var p = processes.get(req.params.id);
 if(!p) return res.end("id not found");
 p.first = req.params.data;
 p.pending(res);
});

app.get("/second/:id/:data",function(req,res){
 var p = processes.get(req.params.id);
 if(!p) return res.end("id not found");
 p.second = req.params.data;
 p.pending(res);
});

这篇关于如何同步2个或更多异步请求的数据并使用解析的数据发送响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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