当我们遇到消息推送功能要如何开发呢?
一、ajax轮询,定时去请求服务器数据 (推荐学习:PHP视频教程)
通过观察thinkphp官网貌似也是用的这个方法,下边将这种方法整理一下:
Notify.php //获取通知消息 public function getNotifyCount() { $msg = db('message_logs')->where('isscan',0)->count(); RestfulTools::restData($msg); //这里是封装好的json_encode方法 }
notify.js <span class="am-icon-envelope-o"></span> 消息 <span class="am-badge am-badge-warning" id="msgCount"> {$msgCount} //这是是通过tp的 assign方法分配过来的变量,作为初始值 </span> <script type="text/javascript"> var getting = { url:"{:url('Notify/getNotifyCount')}", dataType:'json', success:function(res) { console.log(res); var msgCount = res.result; $("#msgCount").html(msgCount); //用js的 html方法去改变id为msgCount的值 } }; //Ajax定时访问服务端,这里是3分钟请求一次。 window.setInterval(function(){ $.ajax(getting) },180000); </script>
以上就是php如何实现消息推送的详细内容,更多请关注php中文网其它相关文章!
声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
- 上一篇:如何用PHP完整的开发一个网站?
- 下一篇:php类怎么实现自动加载
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论