简体   繁体   English

事件发生时获取从Express到angular的http调用

[英]Get an http call from express to angular when event happens

i'm building an angular app that will make about a thousand people to connect simultaneously to book a ticket. 我正在构建一个有角度的应用程序,它将使大约一千人同时连接以预订机票。 I want only "XYZ" of them to access simultaneously at the registration Angular component. 我只希望其中的“ XYZ”在注册Angular组件中同时访问。 The other ones will see a "waiting room" component until it's their turn. 其他人将看到“等候室”组件,直到轮到他们了。 I set up the whole thing like this: 我将整个过程设置如下:

  1. User enters the page. 用户进入页面。
  2. I make an http call to expressjs server 我对ExpressJS服务器进行http调用
  3. The server checks if the "connections" collection constains less than XYZ docs 服务器检查“连接”集合是否包含少于XYZ文档
  4. If true, it unlocks the user registation component and with an http post req, it creates a new doc in the db. 如果为true,它将解锁用户注册组件,并带有一个http post req,它将在数据库中创建一个新文档。 if false it leaves it hidden and shows up the waitingroom component 如果为false,则将其隐藏并显示出等候室组件
  5. When user leaves the page, his doc in "connections" collection gets destroyed with an http delete call. 当用户离开页面时,他的“连接”集合中的文档会被http delete调用销毁。

Fully working. 充分发挥作用。

The problem is now that i want to create a kind of "priority" system, because, going like that, if you just refresh you may be lucky and get access, even if you are soon arrived and there is who is waiting since 1990's. 现在的问题是,我想创建一种“优先”系统,因为像这样,即使您刚刚刷新,也可能很幸运并且可以访问,即使您很快就到了,并且自1990年代以来就有人在等待。 So i introduced a "priority" system. 因此,我引入了“优先级”系统。 When the user makes the first http call, if user is not allowed, the server creates a timestamp and pushes it into an array. 当用户进行第一个http调用时,如果不允许该用户,则服务器会创建一个时间戳并将其推送到数组中。

const timestamps = []
.
.
.
// this below is in http get req
Connessione.countDocuments({},(err,count)=>{
    if(count<=nmax){
      console.log("Ok")
      res.status(200).json({allowed: true})
    }
    else{
      const timestamp = req.params.timestamp;
      timestamps.push(timestamp);
      console.log("Semo troppi")
      res.status(401).json({allowed: false})
    }
  });

The idea is to listen to db changes, and when there is just XYZ-1 in the db. 这个想法是听数据库的变化,以及当数据库中只有XYZ-1时。 Make a call to the first timestamp's angular frontend to say him: "Hey there, if you want we're done. You can go" and unlock him the access to registration component. 打电话给第一个时间戳记的前端,对他说:“嘿,如果您想完成我们的事,可以走了。”然后解锁他对注册组件的访问权限。

The problem is that i can't make continuous http requests every second from angular until there's a free place... Is there any method to send a request at the server, and when server says OK, calls angular and says "Hey dude. You can go!"? 问题是,直到有空闲的地方,我才能每秒从angular发出连续的http请求。是否有任何方法可以在服务器上发送请求,并且当服务器说OK时,调用angular并说“嘿,伙计。你可以走了!”? Hope you understood my question. 希望你理解我的问题。 If not ask me in the comments. 如果没有在评论中问我。 Thanks in advance 提前致谢

Even i had trouble with sockets in the beginning so i'll try to explain the concept in a simple way, Whenever you write an API or Endpoint you have a one way connection ie you send request to server and it return back some response as shown below. 即使我一开始就在套接字方面遇到麻烦,所以我将尝试以一种简单的方式来解释该概念,每当您编写APIEndpoint您都将具有单向连接,即,您将request发送到服务器,并且它返回一些response ,如图所示下面。

Event 1:
(Client) -> Request -> (Server)

Event 2:
(Client) <- Response <- (Server)

For API's, without request you cannot get response. 对于API,如果没有请求,您将无法获得响应。

To overcome this issue as of now i can think of two possible ways. 到目前为止,要克服此问题,我可以想到两种可能的方法。

  1. Using Sockets , With sockets you can create a two way connection. 使用套接字 ,使用套接字可以创建双向连接。 Something like this 像这样

(Server) <-> data <-> (Client)

It means you can pass data both ways, Client to server and Server to client. 这意味着您可以通过两种方式(客户端到服务器和服务器到客户端)传递数据。 So whenever an event occurs(some data is added or updated in database) one can emit or broadcast it to the client and the client can listen to the socket and receive it. 因此,无论何时发生事件(在数据库中添加或更新了一些数据),都可以向客户端发出或广播该事件,并且客户端可以侦听套接字并接收它。

In your case as it's a two connection you can emit the data from angular and I've attached few links at the bottom. 在您的情况下,由于是两个连接,您可以从angular发出数据,而我在底部附加了一些链接。 please have a look. 请看一看。

  1. Using XML/AJAX Request , This is not a preferable method, using setInterval you can call the server in every 5 seconds or so and do the operation needed. 使用XML / AJAX Request ,这不是首选方法,使用setInterval可以每隔5秒左右调用一次服务器并执行所需的操作。
setInterval(ajaxCall, 5000); //5000 MS == 5 seconds

function ajaxCall() {
    //do your AJAX stuff here
}

Links: 链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM