2011-12-29 68 views
2

这已经在很多网站中实现,可以说是odesk左右。我在想的是实现一个消息发送方案,通知用户他们已经收到消息。Yii的消息系统实现

例如

你发信息给我,我会从绿色显示消息红色图标。很明显,我们需要一个数据库表来存储发件人ID接收器ID等,但我们如何实现它,用户不需要点击刷新按钮。

我是新来自c#背景的网站,所以不知道许多方法。

我在Yii开发它。几个建议将是伟大的

回答

3

您可以通过使用简单的periodic refresh JavaScript方法来做到这一点。

像这样的事情在具有消息指示器的观点:

<?php Yii::app()->clientScript->registerScript('autoupdate-div-inbox-update', 
      "setInterval(function(){ 
       // do an ajax call to server to check for new messages 
       // using jquery's ajax method 
       $.ajax({ 
        url: 'http://example.com/index.php?r=controller/action',// this is the url that will check for new messages for the current user 
        success: function(data) {// data is the data returned from the server 
         if(data){ 
          // update your new message div 
          // you can show your red icon here 
         } 
        } 
       }); 
       return false; 
      },1000);" 
    ); 
?> 

所以发生的是,setInterval的方法执行功能每隔1000毫秒和使用AJAX新邮件功能检查。

如果你不知道在警予阿贾克斯,然后检查控制器动作如下:

public function actionMessages(){ 
    // check for new messages in the db 
    $xyz = checkMessage(); 
    // assuming checkMessage returns the number of new messages if any or false if none 
    // whatever we echo will be available to the javascript we wrote in the data variable 
    echo $xyz; 
} 

了解更多关于timing methods in javascript

这种模式也被称为轮询,也有其他流行的方法,如长轮询,并服务器推送,这我不是很熟悉,但你决定的模式之前,也应该检查出来。

希望这会有所帮助,请澄清,如果有的话。

+2

谢谢。该网址可以简单地为:url:'controller/action'。由于我对apache使用端口82,因此手动修改它以适应新的服务器配置可能会感到不舒服。 – Silentbang 2012-01-21 04:04:29

+0

真棒,很好,我也学到了一些东西。 – 2012-01-21 05:05:35

+0

哦,我忘了。 “控制器/操作”仅适用于我们想在视图中实现这一点的情况。我们应该连接字符串,如url:\“”。 CController :: createUrl('controller/action')。 “\”, – Silentbang 2012-01-21 06:10:40