简体   繁体   English

将 Node Express 服务器上长时间运行的异步 function 的进度数据发送到反应客户端

[英]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.

相关问题 如何在React-Express Node App中将对象从服务器发送到客户端组件 - How to send objects from server to client side components in react-express node app 无法将数据从反应发送到快递服务器 - Unable to send data from react to express server 将数据从React传递到节点快递服务器 - Pass Data from React to node express server 如何在节点中从服务器向客户端发送数据? - How to send data from server to client in node? 从服务器向客户端发送数据。 节点JS,快递 - Send data from server to client. NodeJS, Express 如何在Next js上将数据从快递服务器发送到客户端? - How to send data from express server to client on Next js? 我不知道如何在 nodejs express 服务器中存储来自 api 调用的 json 数据,并将其发送到反应本机客户端 - I do not know how to store json data from an api call within a nodejs express server, and send it to a react native client 从 Node/Express 服务器在 React 中播放音频数据 - Playing audio data in React from Node/Express server 从服务器端向客户端节点发送数据 - Send data from server side to client side Node 在node.js和express中,客户端应如何向服务器发送多达3k的大量数据? - In node.js and express, how should a client send a large, up to 3k, amount of data to the server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM