无需重新加载页面(如Facebook或谷歌加通知)的通知 [英] notifications without reloading the page (like facebook or google plus notifications)
问题描述
什么是理想的机制来获得通知像在Facebook中到仪表板?我想最好的办法是做一个Ajax调用PHP页面每5秒和检索的通知。
What would be the ideal mechanism to get notifications like in Facebook in to a dashboard ?. I am thinking the best way is to do a Ajax call to a php page every 5 seconds and retrieve the notifications.
有没有什么更好的方法做了类似的变化?
Is there any better way to do a similar change?
这应该适用于所有手机浏览器了。
It should work in all mobile browsers too.
我这样做以下方式,
使用 $。交
jQuery中为获取数据,而无需页面清爽。
use $.post
in jquery for get data without page refreshing.
$.post("page.php",{"act":1},function(data){
$("#id").html(data);
});
in page.php write your query
修改1
我写了这样的功能指的是一些网上的笔记和实时工作后。
I wrote a function like this after referring to some online notes and its working in realtime.
var TimeStamp = null;
function waitForMsg() {
$.ajax({
type: "GET",
url: "getData.php?timestamp=" + TimeStamp,
async: true,
cache: false,
timeout: 50000, /* Timeout in ms */
// data: "TimeStamp=" + TimeStamp,
success: function( data ) {
var json = eval('(' + data + ')');
if ( json['msg'] != "" ) {
alert( json['msg'] );
}
TimeStamp = json['timestamp'];
setTimeout(
'waitForMsg()', /* Request next message */
1000 /* ..after 1 seconds */
);
},
error: function( XMLHttpRequest, textStatus, errorThrown ) {
alert("error:" + textStatus + "(" + errorThrown + ")");
setTimeout(
'waitForMsg()', /* Try again after.. */
"15000"); /* milliseconds (15seconds) */
},
});
}
;
// calling after dom is ready
$(document).ready(function() {
waitForMsg();
});
PHP文件,
PHP file is,
<?php
$filename = dirname(__FILE__).'/data.txt';
$lastmodif = isset( $_GET['timestamp'] ) ? $_GET['timestamp'] : 0;
$currentmodif = filemtime( $filename );
while ( $currentmodif <= $lastmodif ) {
usleep( 10000 );
clearstatcache();
$currentmodif = filemtime($filename);
}
$response = array();
$response['msg'] = file_get_contents( $filename );
$response['timestamp'] = $currentmodif;
echo json_encode($response);
编辑2
所有的工作很好,但在没有改变happend到的data.txt文件,我得到这样的错误消息,在50秒内。
All work well but when there is no changed happend to data.txt file i get a error message like this in 50 seconds.
错误:超时(超时)
这怎么能是prevented?
how can this be prevented ?
REF: JavaScript变量范围
推荐答案
据我所知,基本上有两种方法可以做到这一点:轮询和WebSockets的。的间隔轮询要么使许多请求,或具有一个很长的请求的浏览器和服务器知道的是长(也称为长轮询)。再有就是WebSockets的。我还没有在一段时间PHP土地,但最后我检查并没有真正支持有WebSockets的。它可能已经改变。在节点的世界socket.io是使用的WebSockets和长轮询作为备份的最佳解决方案。
From what I know, there are basically two ways to do this: polling, and websockets. Polling is either making many requests at an interval, or having a very long request the browser and server know is long (also called long polling). Then there is websockets. I haven't been in PHP land in a while, but last I checked websockets weren't really supported there. It could have changed. In Node world socket.io is a great solution that uses websockets and long polling as a backup.
一个快速搜索发现本作的WebSockets和PHP: http://socketo.me/docs/
A quick search has found this for websockets and php: http://socketo.me/docs/
这篇关于无需重新加载页面(如Facebook或谷歌加通知)的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!