使用 ajax 和 django 实现安全倒计时器

发布于 2024-10-06 01:32:56 字数 854 浏览 0 评论 0原文

我正在尝试在服务器端使用ajax和django制作一个类似于rapidshare中的倒计时器。

我的ajax调用如下:

function loadXMLDoc(url,cfunc){
    if (window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
    }else{
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=cfunc;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}

function getFile(){

    countdown(15);//just shows the counter
    loadXMLDoc("getfiles",function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementById("dlzone").innerHTML=xmlhttp.responseText;
    }
  });
}

在服务器端我有一个测试功能:

def getfile(request):
    sleep(15)
    return HttpResponse("file")

一切都按预期工作,除了它让我想知道是否有更好的方法让服务器休眠15秒。如果我使用 javascript 完成所有等待,那么操作变量并绕过倒计时将非常容易。但让服务器等待仍然感觉不是最好的解决方案。有什么建议吗?

I'm trying to make a countdown timer similar to the one in rapidshare using ajax and django on the server side.

My ajax call is the following:

function loadXMLDoc(url,cfunc){
    if (window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
    }else{
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=cfunc;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}

function getFile(){

    countdown(15);//just shows the counter
    loadXMLDoc("getfiles",function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementById("dlzone").innerHTML=xmlhttp.responseText;
    }
  });
}

and on the server side I have a test function:

def getfile(request):
    sleep(15)
    return HttpResponse("file")

Everything is working as expected except it leaves me wondering if there's a better approach that having the server sleep for 15 seconds. If I did all the waiting using javascript it would be very easy to manipulate variables and bypass the countdown. But still, having the server wait doesn't feel like the best solution. Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春庭雪 2024-10-13 01:32:56

这绝对是错误的做法。您将连接保持打开状态 15 秒,这意味着即使在中等负载的情况下,您也必须能够在服务器上建立大量连接。

相反,您应该调用服务器,只需设置请求有效的时间,然后在客户端上进行倒计时。倒计时完成后,您调用服务器上的另一个函数,该函数在发送文件(或有关文件的信息)之前检查超时(如果太早则返回错误)

This is definitely the wrong way to do it. You are holding the connection open for 15 seconds, which means that you have to be able to take a ton of connections on your server, even in a moderately loaded scenario.

Instead you should make a call to the server, where you simply set a time when the request will become valid, and then on the client you make the count down. When the countdown is done, you call another function on the server, which checks the timeout (and returns an error if it is too early) before sending the file (or info about the file)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文