【问题标题】:Real time notification using angularjs php mysql使用 angularjs php mysql 的实时通知
【发布时间】:2016-05-20 05:04:43
【问题描述】:

我想在我的网站上使用 angular、php 和 mysql 实现像 facebook 这样的实时通知。是否有任何人可以帮助我的参考链接或教程?

另外,有没有其他工具可以在我的网站上实现相同的功能?

【问题讨论】:

  • 在问这些问题之前你应该先用谷歌搜索一下。
  • 对此没有简单的答案。你会想要研究“长轮询”,或者如果你喜欢冒险的图书馆,比如 socket.io

标签: php mysql angularjs real-time


【解决方案1】:

尝试使用 Socket.io,它将帮助您实现通知、聊天或任何实时应用程序。

【讨论】:

  • 有什么教程可以推荐使用 PHP 吗?另外,Internet Explorer 等浏览器是否存在支持问题?
【解决方案2】:

您可以使用这些东西来实现这些功能。

  1. 基于 PHP JSON 的 API
  2. 基于 PHP 套接字的 JSON API
  3. 基于套接字 IO 的 API

所有这些 API 都可以很容易地被 AngularJS 或 jQuery 解析

对于 PHP,如何制作 API

<?php
//Set header for Javascript to recognize it as the JSON output
header('Content-Type:application/json;');
//I am using GET parameters, but POST can also be used and can make amazing APIs
switch($_GET['action']){
    case "addlike":
         //SQL Query passed to add a like as facebook
         //Set the output array to provide json Output, here's the example
         $output['status'] = 200;
         $output['message'] = 'Like Added';
    break;
    case "addcomment":
    break;
}
echo json_encode($output);

要使用上面的代码,URL 应该是:

http://yourserve/youfile.php/?action=addlike

输出将是

{
  "status":200,
  "message":"Like Added"
}

如何在 jQuery 中使用它

/** For Example you have like button with class="btnlike" **/
$('.btnlike').on('click',function(){
    $.get('http://yourserve/youfile.php',{action:'addlike'},function(data){
        if(data['status'] == 200){ alert('You Liked the Post'); }
    });
});

如何在 AngularJS 中使用

app.controller('myCtrl',function($scope,$http){
    $scope.message = '';
    $scope.addlike = function(){
        $http.get('http://yourserve/youfile.php',{action:"addlike"}).success(function(data){
             if(data['status']==200){ $scope.messages = 'You liked the post'; }
        });
    };
});

【讨论】:

  • 这对 Marmik 非常有帮助,请您帮我解决这个问题,比如在 cron 作业执行后显示通知。例如,考虑每 5 分钟执行一次 cron 作业并将一些数据更新到 mysql 表。之后我必须显示实时通知。我怎样才能用角度来实现这一点?因为我没有像按钮点击这样的事件,我该如何调用角度代码??
  • 这有什么性能问题吗?一个高流量的网站怎么样?每秒执行一次 AJAX 请求有什么问题吗?
  • 这是一种社交网络,目前的情况就像你评论了我的照片,我必须得到通知
  • 恐怕无法更改框架。有什么方法可以实现这个 zf2 和 node js 或提到的@Meesam 之类的东西。但是我真的很困惑在共享服务器上实现它,因为我最初将在它上面运行。
  • 所以你建议使用 nodejs 的 socket.io?
猜你喜欢
  • 1970-01-01
  • 2011-06-05
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
相关资源
最近更新 更多