实时刷新/更新

Real Time Refresh / Update

本文关键字:更新 刷新 实时      更新时间:2023-09-26

我有一个MVC 3项目,在服务器上发布。

例如,我有一个保存数据从(PC1)(PC2)的函数。

用户在(PC2)中看到的data(data in jqgrid)可能是open(open in page), (PC1)保存数据后会自动刷新或更新页面或jqgrid。

我的jqgrid版本是4.3.3

希望你们明白我在我的帖子里的意思。如果投票失败,发布反馈。谢谢。

您可能想使用ajax来完成这样的工作,请阅读下面

如果我理解你的意思,是你想轮询服务器实时更新的间隔或其他东西…

选项1
向服务器发出一个正常的无状态ajax调用,然后强制服务器在有限的时间内保持请求[以克服服务器开销]

这也可以称为反向ajax或comet。
除非你打算使用websocket技术,否则我不建议你这么做。

if(isset($_GET['finite'])){
#declare time for a session
$_SESSION['typing']=$reduce_browser_overhead=time();
#remember to close the session before entering the loop;
#if u dont close, then the browser will not reload the same website untill the connection is close or satisfied
session_write_close();
function loop(){
    #do this to access external variables  ===> $reduce_browser_overhead;
    global $con,$reduce_browser_overhead;
    #explicitly check 2exit
    #please do this to release mysql connection since they are in a loop
    if($reduce_browser_overhead+4<time()){
        echo ' ';ob_flush();flush();
        if(connection_aborted()){
            #do some work here before you finally exit the connection
            exit;
        }$reduce_browser_overhead=time();
    }
    #php prepare statement...
    $looper=$con->prepare("SELECT ROW FROM TABLE WHERE ID=SOMETHING AND $_SESSION[typing]=SOME_ROW");
    #The statement above willcause the loop to work
    #If a table had been update and table has not yet updated, this sql will detemin by the current time
    #meaning that if the time[integer] of SOME_ROW is not equal to the time in the session variable,
    #then it will let go to the client and then again it will continue looping untill the time in the
    #SOME TABLE ROW  changes....
    $looper->execute();
    $looper->store_result();
    if($looper->num_rows>0){
        sleep(2);
        #do some work before looping again
        loop();
        #you have to explicitly return to this loop to work as expected.
        return;
    }else{
        #send back data to the user or the client listening on the connection
        session_start();$_SESSION['typing']=time();session_write_close();
        #update the session before finishing the request so that the next time the request comes, the time will be equal to the DBserver time in the row and hence causing the loop again and again => more like a cycle
        echo 'After some time the server has received new data which is =>> '.$newdata;
    }
}loop();
exit;

 //In another file on in the same document as the php / your server file  ==> do javascript below
    //first issue a normal / stateless ajax request to the target server
    $.ajax({
        //All optional but url required!
        url:'abc.php?var_one=blabla',
        cache:true,//whether to cache the requests
        timeout:(1000*60)*20,//timeoutthe request
        success:function(data){
            //if the server successfully completed the request
            //do some work here with data returned
        },
        error:function(){
            //if the server return an error
            //do more work around
            //or call the function again
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>