Php 约会网站的macos Growl/facebook等通知-从哪里开始?
大家好,我正在运行一个php/mysql交友网站,我想在某些操作上为成员实现屏幕通知(例如,当其他成员查看您的个人资料、向您眨眼等)。非常类似于当有人在你的个人资料上发表评论或写文章时在mac或facebook通知上咆哮 我真的不太确定从哪里开始——我必须在服务器端实现一些推送机制吗?有用于客户端的jquery插件吗 提前感谢。我们的方法是每隔一段时间从数据库中提取信息,并将其显示给用户。对你来说,这可能就足够了。例如:Php 约会网站的macos Growl/facebook等通知-从哪里开始?,php,jquery,notifications,push-notification,Php,Jquery,Notifications,Push Notification,大家好,我正在运行一个php/mysql交友网站,我想在某些操作上为成员实现屏幕通知(例如,当其他成员查看您的个人资料、向您眨眼等)。非常类似于当有人在你的个人资料上发表评论或写文章时在mac或facebook通知上咆哮 我真的不太确定从哪里开始——我必须在服务器端实现一些推送机制吗?有用于客户端的jquery插件吗 提前感谢。我们的方法是每隔一段时间从数据库中提取信息,并将其显示给用户。对你来说,这可能就足够了。例如: 跟踪你想要通知的事情。假设用户A访问用户B的配置文件,将通知消息添加到用户
// set an interval to run a function every 5 minutes
// (300000 = 1000 * 60 seconds * 5 minutes)
setInterval(function() {
// call on check.php
$.post('check.php', {
// add a variable with the userId,
// so the script knows what to check
userId: 123
}, function(data) {
// say there are notifications, which are stored in data
// now display them in any way you like
});
}, 300000);
check.php
$userId = $_POST['userId'];
// make sure to only show new messages
$notifications = array();
$sql = "SELECT message FROM notifications WHERE userId = $userId AND new = 1";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res)) {
while ($r = mysql_fetch_object($res)) {
$notifications[] = $r->message;
}
}
// now make sure that all the notifications are set to new = 0
$sql = "UPDATE notifications SET new = 0 WHERE userId = $userId";
mysql_query($sql) or die(mysql_error());
// you can handle the markup wherever you want,
// e.g. here and simply show each message a new line
// this data will be returned to the jQuery $.post method
echo implode('<br />', $notifications);
$userId=$\u POST['userId'];
//确保只显示新消息
$notifications=array();
$sql=“从通知中选择消息,其中userId=$userId,new=1”;
$res=mysql\u query($sql)或die(mysql\u error());
if(mysql_num_行($res)){
而($r=mysql\u fetch\u object($res)){
$notifications[]=$r->message;
}
}
//现在确保所有通知都设置为new=0
$sql=“更新通知集新=0,其中userId=$userId”;
mysql_query($sql)或die(mysql_error());
//您可以随时随地处理标记,
//例如,在此处,只需为每条消息显示一行新行即可
//此数据将返回到jQuery$.post方法
回声内爆(“
”,$notifications)代码>
所以基本上:JS每5分钟调用一次“script.php”,“script.php”可能会向JS函数返回新的通知,您可以显示这些消息。这只是给你一个可能的解决方案的想法
真正的推送通知比看起来更难。这需要一个持续的开放连接,允许新消息立即发送给用户。你需要看看类似的东西。但在大多数情况下,如果有轻微的延迟,其实并不重要。在我的示例中,我使用了5分钟,但也可能是1分钟、10分钟或30分钟。这还取决于您拥有的用户数量以及这些定期检查将导致什么样的负载。但通常所有这些都发生在几分之一秒之内,并且可以很容易地在一个间隔内完成,即使间隔很短。方法是每隔一段时间从数据库中提取信息,并将其显示给用户。对你来说,这可能就足够了。例如:
跟踪你想要通知的事情。假设用户A访问用户B的配置文件,将通知消息添加到用户B的通知表中。如果用户C向用户B眨眼,则将另一个通知添加到用户B的列表中
检查是否有新的通知。检索到通知后,将其标记为已读,这样它们就不会再出现
JS
// set an interval to run a function every 5 minutes
// (300000 = 1000 * 60 seconds * 5 minutes)
setInterval(function() {
// call on check.php
$.post('check.php', {
// add a variable with the userId,
// so the script knows what to check
userId: 123
}, function(data) {
// say there are notifications, which are stored in data
// now display them in any way you like
});
}, 300000);
check.php
$userId = $_POST['userId'];
// make sure to only show new messages
$notifications = array();
$sql = "SELECT message FROM notifications WHERE userId = $userId AND new = 1";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res)) {
while ($r = mysql_fetch_object($res)) {
$notifications[] = $r->message;
}
}
// now make sure that all the notifications are set to new = 0
$sql = "UPDATE notifications SET new = 0 WHERE userId = $userId";
mysql_query($sql) or die(mysql_error());
// you can handle the markup wherever you want,
// e.g. here and simply show each message a new line
// this data will be returned to the jQuery $.post method
echo implode('<br />', $notifications);
$userId=$\u POST['userId'];
//确保只显示新消息
$notifications=array();
$sql=“从通知中选择消息,其中userId=$userId,new=1”;
$res=mysql\u query($sql)或die(mysql\u error());
if(mysql_num_行($res)){
而($r=mysql\u fetch\u object($res)){
$notifications[]=$r->message;
}
}
//现在确保所有通知都设置为new=0
$sql=“更新通知集新=0,其中userId=$userId”;
mysql_query($sql)或die(mysql_error());
//您可以随时随地处理标记,
//例如,在此处,只需为每条消息显示一行新行即可
//此数据将返回到jQuery$.post方法
回声内爆(“
”,$notifications)代码>
所以基本上:JS每5分钟调用一次“script.php”,“script.php”可能会向JS函数返回新的通知,您可以显示这些消息。这只是给你一个可能的解决方案的想法
真正的推送通知比看起来更难。这需要一个持续的开放连接,允许新消息立即发送给用户。你需要看看类似的东西。但在大多数情况下,如果有轻微的延迟,其实并不重要。在我的示例中,我使用了5分钟,但也可能是1分钟、10分钟或30分钟。这还取决于您拥有的用户数量以及这些定期检查将导致什么样的负载。但通常所有这些都发生在几分之一秒之内,并且可以很容易地在一段时间内完成,即使时间很短。我回答这个问题可能有点晚,但是如果有人来这里寻找与这种用例相关的客户端插件,我制作了一个开源jQuery通知系统,可以与web应用无缝集成,名为。您可以在该链接上看到演示。密码接通了。我试图保持API的干净和简单易用。下面是一个例子:
$.notify_osd.create({
'text' : 'Hi!', // notification message
'icon' : 'images/icon.png', // icon path, 48x48
'sticky' : false, // if true, timeout is ignored
'timeout' : 6, // disappears after 6 seconds
'dismissable' : true // can be dismissed manually
});
您甚至可以为将来的所有通知设置全局默认值(可以在每个通知的基础上覆盖):
更新[2012年12月13日]:
已经有一段时间了,但我最终实现了对使用队列系统的多个可见通知的支持。例如:
$.notify_osd.setup({
// ... config ...
'visible_max' : 5 // max 5 notifications visible simultaneously
'spacing' : 30 // spacing between consecutive notifications
});
你可以看到一个演示。我认为该插件现在已经足够灵活,可以覆盖各种各样的用例。我回答这个问题可能有点晚,但是如果有人来这里寻找与这种用例相关的客户端插件,我已经制作了一个开源jQuery通知系统,可以与web应用无缝集成,名为。您可以在该链接上看到演示。密码接通了。我试图保持API的干净和简单易用。下面是一个例子:
$.notify_osd.create({
'text' : 'Hi!', // notification message
'icon' : 'images/icon.png', // icon path, 48x48
'sticky' : false, // if true, timeout is ignored
'timeout' : 6, // disappears after 6 seconds
'dismissable' : true // can be dismissed manually
});
你