用于服务器操作的 Node.js 客户端进度指示器 [英] Node.js client-side progress indicator for server operations

查看:41
本文介绍了用于服务器操作的 Node.js 客户端进度指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 node.js 服务器将对上传的文件执行某些操作,我想近乎实时地将进度反映回客户端.完成此操作大约需要 30 秒 - 1 分钟.

My node.js server will perform certain operations to uploaded files and I would like to reflect the progress in near real-time back to the client. This operation takes about 30 seconds - 1 minute to complete.

理想情况下,在文件上传时,客户端将能够观察到服务器对文件的操作进度.

Ideally, upon file upload, the client will be able to observe the progress of the server operation on the file.

我可以执行常规轮询、长轮询或通过 websockets 连接到服务器,但所有这些似乎都需要为 30 秒到 1 分钟的进度指示器进行相当复杂的设置.

I could either perform a regular poll, long poll, or connect to the server via websockets, but all these seem to require rather complicated set up for a 30sec-1min progress indicator.

有更好的方法吗?

推荐答案

我会用 socket 做到这一点.io.可能有更好的方法,但 socket.io 相对容易配置.

I would do this with socket.io. Better methods may be available, but socket.io is relatively easy to configure.

你在使用 Express.js 吗?如果是这样,请确保查看特定于 Express 的部分以获取有关配置的详细信息.配置完成后,您需要执行以下操作:

Are you using Express.js? If so make sure you look at the section specific to Express for details on configuration. Once configured you'll want to do the following:

在服务器上,您需要发出一个新状态

On the server, you will want to emit a new status

socket.emit('status','aok!');

在客户端,您需要监听来自服务器的新状态

On the client, you will want to listen for a new status from the server

socket.on('status', function (msg) {
    console.log(msg);
    //Put code here to update the page!
});

您可能需要考虑的是调整心跳时间.基本上,服务器每隔一段时间就 ping 客户端以检查它们是否仍然连接.我相信默认情况下,这每 25 秒发生一次,超时发生,60 秒后没有响应.您可能希望延长心跳之间的时间,甚至完全禁用它们.

Something you may want to consider is adjusting the heartbeat time. Basically the server pings the client every so often to check if they are still connected. I believe by default this happens every 25 seconds with a timeout occurring with no response after 60 seconds. You may want to lengthen the time between heartbeats, or even disable them entirely.

这篇关于用于服务器操作的 Node.js 客户端进度指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆