简体   繁体   English

Express 不允许来自同一 IP 的多个待处理请求?

[英]Express not allowing more than one pending request from the same IP?

I'm making a chat application that uses long polling with express (I'm aware websockets are better for this, I just wanted to make something specifically with long polling).我正在制作一个使用 express 的长轮询的聊天应用程序(我知道 websockets 对此更好,我只是想专门用长轮询来做一些事情)。

This is the code that the client sends a GET request to when it's waiting for a new chat message:这是客户端在等待新聊天消息时向其发送GET请求的代码:

app.get('/api',(req,res)=>{
    listeners.push(res)
})

It pushes the response objects to an array, and once a new chat message is available it calls .send() on the responses.它将响应对象推送到一个数组,一旦有新的聊天消息可用,它就会在响应上调用.send() When the client recieves the response, it sends a GET request again, waiting for new messages.客户端收到响应后,再次发送GET请求,等待新消息。

Here's the code for when someone sends a message (simplified)这是某人发送消息时的代码(简化)

app.post('/api', (req,res)=>{
    listeners.forEach(listener=>{
        listener.json(req.body)
    })
    listeners = []
    res.status(200).end()
})

I noticed that when I open two tabs of my site, only the 1st tab's request gets put in the array at first.我注意到,当我打开网站的两个选项卡时,首先只有第一个选项卡的请求被放入数组中。 That makes the 2nd tab not receive the 1st chat message.这使得第二个选项卡不会收到第一条聊天消息。 The 2nd tab's request only gets pushed into the array after the 1st chat message.第二个选项卡的请求仅在第一条聊天消息之后被推送到数组中。 So when the second chat message is sent it works fine.因此,当发送第二条聊天消息时,它工作正常。 This means that the 2nd tab is always one request behind.这意味着第二个选项卡总是落后一个请求。

Here's an example in case that wasn't clear:如果不清楚,这是一个示例:

Client 1 connects
Client 2 connects
---
Client X sends a message:
Client 1's request 1 receives the message
Client 2's request 1 is still pending
---
Client X sends another message
Client 1's request 2 receives the message
Client 2's request 1 receives the message
---
Client X sends another message
Client 1's request 3 receives the message
Client 2's request 2 receives the message
...

Another thing I noticed is that when I restart the server after client 1 and 2 connect, client 2's request get pushed into the array after the restart.我注意到的另一件事是,当我在客户端 1 和 2 连接后重新启动服务器时,客户端 2 的请求在重新启动后被推送到阵列中。

Could someone explain this behavior?有人可以解释这种行为吗? Thanks in advance.提前致谢。

This sounds like the browser is hoping to use a cached request so it waits for a previous request with the exact same URL to see if it can use the cached result rather than firing the same request again.这听起来像是浏览器希望使用缓存的请求,因此它会等待具有完全相同 URL 的先前请求,以查看它是否可以使用缓存的结果,而不是再次触发相同的请求。

You can test this hypothesis by adding a unique query parameter to every request as that will disable any attempts at caching since they would all be different URLs.您可以通过向每个请求添加一个唯一的查询参数来测试这个假设,因为这将禁用任何缓存尝试,因为它们都是不同的 URL。

Or, if these requests are sent using the fetch() api in the browser, you can directly tell that API to not use caching with the cache: 'no-cache' option as in:或者,如果这些请求是使用浏览器中的fetch() api 发送的,您可以直接告诉该 API 不使用带有cache: 'no-cache'选项的缓存,如下所示:

const response = await fetch(url, {cache: 'no-cache'});

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

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