AS3:计算当前上传速度(或吞吐量)
我正在使用 FileReference 类的 upload() 方法上传文件。我想显示当前的连接速度,我想知道什么是做到这一点的好方法。
我当前的技术是每 1 毫秒使用一个计时器,如下所示:
var speed:Function = function(event:TimerEvent):void {
speed = Math.round((currentBytes - lastBytes) / 1024);
lastBytes = currentBytes;
}
var speedTimer:Timer = new Timer(1000);
speedTimer.addEventListener(TimerEvent.TIMER, uploadSpeed);
并将 currentBytes 设置到 ProgressEvent.PROGRESS 中。这种技术似乎不精确。我想知道还有什么其他方法可以在上传时计算上传速度并实时显示。
欢迎任何想法或意见!
非常感谢你,
鲁迪
I am uploading files using the upload() method of the FileReference class. I want to display the current connection speed and I was wondering what was a good way to do that.
My current technique is to use a Timer every 1 mili second such as follows:
var speed:Function = function(event:TimerEvent):void {
speed = Math.round((currentBytes - lastBytes) / 1024);
lastBytes = currentBytes;
}
var speedTimer:Timer = new Timer(1000);
speedTimer.addEventListener(TimerEvent.TIMER, uploadSpeed);
and currentBytes gets set into the ProgressEvent.PROGRESS. This technique seems imprecise. I was wondering what other ways I could use to calculate the upload speed while uploading and display it in real time.
Any ideas or opinions are welcomed!
Thank you very much,
Rudy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果该代码块是复制并粘贴的,那么它肯定不会像您预期的那样工作。您将
speed
声明为一个函数,在该函数中您似乎将其重新定义为一个数字。我很欣赏 Flash IDE 让您摆脱粗略的语法,但这样的代码会给您带来各种麻烦。编写代码时尽量明确。尝试这样的操作,将
yourLoader
替换为您分配给上传者的任何标识符:这应该计算在 1 秒 间隔内移动的字节数。
编辑:
您可能希望将间隔设置为略小于 1000 毫秒,并计算最后 n 个样本的平均速度。这将使您的用户看到的数字看起来比现在更稳定。将 speed 设为
Array
并将.push()
设为最新示例。使用.shift()
删除最旧的样本,这样就不会损失太多准确性。反复试验将使您更好地了解要保留多少样本以及采集样本的频率。If that code block is a copy and paste it certainly won't work as you have expected it to. You declare
speed
as a function within which you appear to redefine it as a number. I appreciate the Flash IDE let's you get away with sketchy grammar, but code like that is going to lead you into all kinds of trouble. Try to be explicit when writing your code.Try something like this, replacing
yourLoader
with whatever identifier you assigned to the uploader:That should calculate how many bytes moved in 1 second intervals.
Edit:
You might like to make the interval a little smaller than 1000ms and calculate an average speed for your last n samples. That would make the number your users see appear more stable than it probably does right now. Make speed an
Array
and.push()
the latest sample. Use.shift()
to drop the oldest samples so that you don't lose too much accuracy. Trial and error will give you a better idea of how many samples to hold and how often to take them.您可以监控服务器上的上传速度,然后将该数据发送回客户端。此技术通常用于 ajax 文件上传表单。
You can moniter the upload speed on the server and then send that data back to the client. This technique is often used for ajax file upload forms.