2013-10-23 79 views
0

我发现将近两天,找不到关于c#客户端长轮询的任何教程。任何人都可以给我一个例子或帮助我解决这个问题。所以,这是我的PHP文件。c#php客户端长轮询

b.php

<? 
set_time_limit(0); 
    $file = 'test.txt'; 
    $js_time = !empty($_GET['time']) ? intval($_GET['time']) : 0; 
    $file_time = filemtime($file); 
    while($file_time <= $js_time){ 
    usleep(10000); 
    clearstatcache(); 
    $file_time = filemtime($file); 
    } 
    $info = file_get_contents($file); 
    echo $info; 
    ?> 

Second.php

function test(){ 
    var aj; 
    try{aj=new ActiveXObject("Msxml2.XMLHTTP");} 
    catch(e){ 
     try{aj=new ActiveXObject("Microsoft.XMLHTTP");} 
     catch(E){aj=false;} 
    } 
    var time = Math.round(new Date().getTime()/1000); 
    if(!aj&&typeof XMLHttpRequest!=undefined)aj=new XMLHttpRequest(); 
    aj.open("GET","b.php?time="+time,true); 
    aj.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8'); 
    aj.onreadystatechange=function(){ 
     if(aj.readyState==4&&aj.status==200){ 
      document.getElementById("test").innerHTML = aj.responseText; 
      test(); 
      //alert('aa'); 
     } 
    } 
    aj.send(null); 
} 
test(); 
</script> 

C#

string URL = "http://localhost/gcm_server_php/b.php?time=" + DateTime.Now.ToString() ; 
     WebClient webClient = new WebClient(); 

     string responseBytes = webClient.DownloadString(URL); 
     string responsefromserver = responseBytes; 
      a.Content=responsefromserver; 
     webClient.Dispose(); 

使用此C#代码只能获得一次。任何人都可以帮助我使用c#来长时间轮询b.php吗?感谢帮助,并为我可怜的英语感到抱歉。 :(

+0

这里是C#在这个 – Patel

+1

如果您使用的是C#,有你看着signalr – Fisch

+0

编辑我的问题,已经贴上我的C#代码请再次检查!非常感谢! – user2739955

回答