使用iframe中的英寸长轮询" withouth的产生闪烁的浏览器 [英] Using iframe for "long polling" withouth causing flashing browser

查看:119
本文介绍了使用iframe中的英寸长轮询" withouth的产生闪烁的浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查,如果命令被写入到一个txt文件,以更新我的网页浏览器内的三维窗口。这就是所谓的的技术或长轮询以通知客户端。随着浏览器的必须 Internet Explorer的我有点有限。

I need to check if commands are written to a txt file in order to update a 3D window inside my web browser. This is what is known as "push" technique or long polling to notify the client. As the browser has to be Internet Explorer I am a bit limited.

我想出了使用隐藏的iframe调用重新加载每秒检查txt文件PHP脚本的解决方案。

I have come up with a solution using a hidden iframe that calls a php script that reloads every second to check the txt file.

<iframe noresize scrolling="no" frameborder="0" name="loader" src="loader.php">  

在loader.php基本上做到这一点:

The loader.php basically does this:

//check txt and get commands
<body onLoad="window.setInterval('location.reload()',1000);"></body>

我看到的唯一的问题是,每一秒在网络浏览器的刷新按钮闪烁。尽管此窗口不闪烁,只是按钮,我仍然觉得有点讨厌。

The only problem I see is that every second in the web browser the reload button flashes. Although the window is not flashing, just the button, I still find it a bit annoying.

是否有此问题的一个更好的解决办法,还是与IE浏览器兼容?

Is there a better solution for this problem, still compatible with IE?

推荐答案

最后,许多尝试后,我设法使它工作,我认为最佳的标准溶液:一个长轮询Ajax解决方案。由于这给在IE7很多问题,我会贴上我的codeS下面。

Finally, after many tries I managed to make it work which I consider the optimal standard solution: a long polling ajax solution. As this gives many problems in IE7, I will paste my codes below.

首先我的PHP文件,该文件中读取一个txt和呼应的JSON的文本。

First my PHP file, which reads a txt and echoes a JSON with the text.

<?php
$commandFile = fopen("./command.txt","r");

fseek($commandFile, 0);
$command = fscanf($commandFile, "%s\n"); //reads the command
fclose($commandFile);

if($command[0] != "")
{
  $commandFile = fopen("./command.txt","w"); //delete first line
  fclose(commandFile);    
  echo json_encode($command[0]);  
}
?>

现在,该HTML主页需要一种机制来调用一个函数递归的launchs这个PHP文件,并检查了答案。我的功能是这样的。

Now, the HTML main page needs a mechanism to call a function recursively that launchs this PHP file and checks the answer. My function is something like this.

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<script type="text/javascript">
function longPolling()
{
    $.ajaxSetup({ cache: false }); //This line is THE KEY to work under Internet Explorer
    $.ajax({                
        type: "GET",
            url: "loader.php",
        dataType: 'json',
        async: true,

        success: function(response){                                
            if(response!=""){
                sendCommand(response);  //could be any other function           
            }
            //longPolling();
            setTimeout(longPolling,1000);
                },
        error: function(){
            //longPolling();            
            setTimeout(longPolling,1000);
        }

           });
    };

$(document).ready(function(){   /*waits till the whole page loads*/
    longPolling(); /*start the initial request*/
});
</script> 
</body>

这篇关于使用iframe中的英寸长轮询&QUOT; withouth的产生闪烁的浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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