[英]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.有几点要记住。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.