简体   繁体   English

在 Node.js 中处理来自 Swift 客户端的长时间运行请求

[英]Handle long-running request from Swift Client in Node.js

So I'm currently using socket.io for a loading screen and it just isn't working so I'm going to try a different solution.所以我目前正在使用 socket.io 作为加载屏幕,但它无法正常工作,所以我将尝试不同的解决方案。 I need the following to happen: user opens up my app--this triggers a function on the server side (CPU intensive scraping function that takes about 8 seconds).我需要发生以下情况:用户打开我的应用程序——这会在服务器端触发 function(CPU 密集型抓取 function 大约需要 8 秒)。 As soon as function is done, I need to notify the client that the function is done and the client fetches the results (after this result is fetched the app loading screen closes and the user enters the app).一旦 function 完成,我需要通知客户端 function 完成并且客户端获取结果(获取此结果后,应用加载屏幕关闭并且用户进入应用)。

My current implementation is using socket.io emissions and processing the scraping function via Redis queue on a background thread.我当前的实现是使用 socket.io 排放并在后台线程上通过 Redis 队列处理抓取 function。

Is there a different approach to doing this instead of a socket connection and sending emissions from client -> server and vice versa?是否有不同的方法来执行此操作,而不是使用套接字连接并从客户端-> 服务器发送发射,反之亦然?

I don't think I can just to a classic GET request, because isn't it bad practice to leave a long running (8 second) request open for so long while waiting for response?我认为我不能只处理经典的 GET 请求,因为在等待响应的同时让长时间运行(8 秒)的请求打开这么长时间不是不好的做法吗? Also, I looked into Server-Sent Events but I'm not sure I would be able to use it in this case.另外,我查看了服务器发送的事件,但我不确定在这种情况下是否能够使用它。

Any insight is much appreciated!非常感谢任何见解!

8 seconds is longer than normal for a response from a "healthy" server, but still should be fine.来自“健康”服务器的响应比正常时间长 8 秒,但应该没问题。 I'd just use a GET from the mobile client, and write it to not block the UI while it is waiting.我只是使用来自移动客户端的 GET,并将其写入以在等待时不阻塞 UI。

Write your UI so that it informs the user that it is waiting for a response, and maybe even give the user an idea of how long the response is likely to take.编写您的 UI,以便通知用户它正在等待响应,甚至可能让用户了解响应可能需要多长时间。

A few points to keep in mind.有几点要记住。

  1. nodejs can usually do a lot of concurrent web-scraping: most web-scraping elapsed time is spent waiting for the scraped servers to respond. nodejs 通常可以做很多并发的网页抓取:大部分网页抓取经过的时间都花在等待被抓取的服务器响应上。 nodejs is asynchronous. nodejs 是异步的。 So worker threads may not help much.所以工作线程可能没有多大帮助。 They definitely will make your server app more complex.它们肯定会使您的服务器应用程序更加复杂。 A good choice for scaling this out might be clustering your nodejs app.扩展它的一个不错的选择可能是集群你的 nodejs 应用程序。
  2. There's no harm in writing your server to stall the response to a GET request for a few seconds, except harm to the user experience .编写您的服务器以将 GET 请求的响应延迟几秒钟并没有什么害处,除了对用户体验的损害 If your user has something reasonable to look at for those few seconds, you can probably get away with this.如果您的用户在那几秒钟内有一些合理的东西可以查看,那么您可能会侥幸逃脱。
  3. If your user is looking at a web page during that stall, you can usexhr or fetch from Javascript code in the page to retrieve that data while you entertain them with a spinner or some such user interface stuff.如果您的用户在该停顿期间正在查看 web 页面,您可以使用xhr或从页面中的 Javascript 代码获取该数据,同时使用微调器或一些此类用户界面的东西来娱乐他们。

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

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