[英]Send progress data from a long running async function on a Node Express Server to a react client
I want to be able to send some kind of progress data from a long running async function running on a node express server when a client requests for the data and waits for its completion.当客户端请求数据并等待其完成时,我希望能够从节点快速服务器上运行的长时间运行的异步 function 发送某种进度数据。
const express = require('express');
const app = express();
const port = process.env.PORT;
app.get('/', (req, res) => {
// Send data to client
(async () => {
await someFunc();
})();
})
Assuming someFunc() is responsible to return the progress data and the final response to the client.假设someFunc()负责向客户端返回进度数据和最终响应。
Well, you can't do that since the first response will end the HTTP request (end the connection).好吧,您不能这样做,因为第一个响应将结束 HTTP 请求(结束连接)。 What you looking for is WebSocket, with WebSocket you can keep pushing data to the client since it maintains the connection
您要查找的是 WebSocket,使用 WebSocket,您可以继续将数据推送到客户端,因为它保持连接
check https://www.npmjs.com/package/websocket检查https://www.npmjs.com/package/websocket
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.