使用php实现反向Ajax

发布于 2024-10-09 19:39:06 字数 134 浏览 9 评论 0原文

我希望在使用 PHP 和 jquery 的应用程序中实现反向 ajax。我用谷歌搜索了一下,发现了 XAJA,但这似乎是一个付费应用程序。是否有可用的开源应用程序或者有人实现了它?

一些指示或提示会非常有帮助。

提前致谢。

I am looking to implement reverse ajax in my application which is using PHP and jquery. I have googled a bit about it and found XAJA but that seems to be a paid application. Is there an open source application available for the same or has someone implemented it?

Some pointers or hints would be very helpful.

Thanks in advance.

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

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

发布评论

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

评论(4

再见回来 2024-10-16 19:39:06

我知道有两种类型的反向 AJAX:
1- 投票
2-推送

我认为轮询更容易实现,你只需让你的javascript每次间隔向服务器发出定期请求,当服务器有一些数据时它就会响应。它就像 ping,有些人称之为心跳,但它是这个问题的非常明显的解决方案。然而,它很容易使服务器过载。

编辑简单轮询示例代码:
服务器端:

<?php
//pong.php php isn't my main thing but tried my best!
$obj = new WhatsNew();
$out = "";
if ($obj->getGotNew()){
    $types = new array();
    foreach ($obj->newStuff() as $type)
        {
            $new = array('type' => $type);
            $types[] = $new;
        }

    $out = json_encode($types);
}
else{
    $out = json_encode(array('nothingNew' => true));
}

客户端:

function ping(){
    $.ajax(
        {

            url : "pong.php",
            success : function (data){
                data = JSON.parse(data),
                if (data['nothingNew'])
                    return;
                for(var i in data){
                    var type = data[i]['type'];
                    if (type && incomingDataHandlers[type]){
                        incomingDataHandlers[type]();
                    }
                }


        });
}
incomingDataHandlers = {
    comments: function () {
        $.ajax({
            url: "getComments.php",
            method: "GET",
            data: getNewCommentRequsetData() // pass data to the server;
            success : function (data){
                //do something with your new comments
            }
        });
    },
    message: function (){
        $.ajax({
            url: "getMessages.php",
            method: "GET",
            data: getNewMessageRequestData() // pass data to the server;
            success : function (data){
                //do something with your new messages
            }
        });
    }
}
$(docment).ready(function () {
    setInterval(ping, 1000);
})

I know of two types of reverse AJAX:
1- Polling
2- Pushing

I think polling is rather easier to implement, you just have your javascript make a regular request to the server every time interval, and when the server have some data for it it will respond. Its like a ping and some call it heartbeat, but its the very obvious solution for this problem. However it may easily overload the server.

EDIT Simple polling Example code:
Server-Side:

<?php
//pong.php php isn't my main thing but tried my best!
$obj = new WhatsNew();
$out = "";
if ($obj->getGotNew()){
    $types = new array();
    foreach ($obj->newStuff() as $type)
        {
            $new = array('type' => $type);
            $types[] = $new;
        }

    $out = json_encode($types);
}
else{
    $out = json_encode(array('nothingNew' => true));
}

Client-Side:

function ping(){
    $.ajax(
        {

            url : "pong.php",
            success : function (data){
                data = JSON.parse(data),
                if (data['nothingNew'])
                    return;
                for(var i in data){
                    var type = data[i]['type'];
                    if (type && incomingDataHandlers[type]){
                        incomingDataHandlers[type]();
                    }
                }


        });
}
incomingDataHandlers = {
    comments: function () {
        $.ajax({
            url: "getComments.php",
            method: "GET",
            data: getNewCommentRequsetData() // pass data to the server;
            success : function (data){
                //do something with your new comments
            }
        });
    },
    message: function (){
        $.ajax({
            url: "getMessages.php",
            method: "GET",
            data: getNewMessageRequestData() // pass data to the server;
            success : function (data){
                //do something with your new messages
            }
        });
    }
}
$(docment).ready(function () {
    setInterval(ping, 1000);
})
凌乱心跳 2024-10-16 19:39:06

您正在寻找他们所谓的“长轮询” - 我做了一个“长轮询 php”,我在堆栈溢出上得到了这个线程:

如何实现基本的“长轮询”?

You are looking for what they call "long poll" - I did a "long poll php" and I got this thread on stack overflow:

How do I implement basic "Long Polling"?

烟酒忠诚 2024-10-16 19:39:06

你可以将 websockets 与“flash”websockets 结合使用,因为几乎所有浏览器都内置了 flash(平均约为 96%? => http://www.statowl.com/flash.php) => https://github.com/gimite/web-socket-js。您可以将其与 http://code.google.com/p/phpwebsocket/ 一起使用。我仍然想知道性能是否会很好。如果可能的话,我会使用node.js 来执行反向ajax。 http://socket.io 是一个非常酷的项目!

you could websockets in conjuction with "flash" websockets because almost all browser have flash on board(average around 96%? => http://www.statowl.com/flash.php) => https://github.com/gimite/web-socket-js. You could use this together with http://code.google.com/p/phpwebsocket/. Still I am wondering if the performance is going to be any good. If it all possible I would use node.js to do reverse ajax. http://socket.io is a really cool project to do this!

北城半夏 2024-10-16 19:39:06

您检查过 APE 吗?

它是一种基于单个低容量 Ajax 连接的基于推送的实时数据流技术。这个概念很有用,您可以通过服务器端实现复制相同的东西

Have you checked APE ?

Its a push based real-time data streaming technology over a single low volume ajax connection. The concept is useful, you may be able to replicate the same thing with your server-side implementation

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