简体   繁体   English

数据库更新时发出警报消息

[英]alert message when database updated

I am creating a Chat application in php. 我正在用php创建一个聊天应用程序。

AIM: when a user1 invites user2 , user2 want to get a alert or popup message that user1 has invites him. 目的:当USER1邀请用户2, 用户2想要得到的是USER1有邀请他警告或弹出消息。

What i have done is - 我所做的是-

When user1 invites, the message is stored in database. user1邀请时,消息将存储在数据库中。 In user side i used to check whether Database is updated or not. 在用户端,我曾经检查数据库是否已更新。 If database is update i used to show alert. 如果数据库是更新的,我通常会显示警报。 This i done on php and javascript but not working all time. 这是我在php和javascript上完成的,但并非一直都在工作。

Is this a good method? 这是个好方法吗? Any other method? 还有其他方法吗?

Use Ajax to send request to Server periodically. 使用Ajax定期向服务器发送请求。 Response will contain any invites that user has. 响应将包含该用户拥有的所有邀请。 Response maybe in JSON which can be easily evaluated in JavaScript. 响应可能采用JSON ,可以使用JavaScript轻松评估。

All the clients can use jquery and send following request periodically: 所有客户端都可以使用jquery并定期发送以下请求:

function checkIfIAmInvited()
{
     jQuery.ajax({
       type: "POST",
       url: "some.php",
       data: "name=currentUser&",
       success: function(msg){
         if(msg.indexOf('uninvited') !== -1 )
         {
            alert( "You have been invited by " + msg );
            //Method to do stuff once I am invited
            iAmInvitedMethod();
         }
     });
}

To call above code periodically you can use following code in jquery/javascript. 要定期调用以上代码,可以在jquery / javascript中使用以下代码。 This would be making periodic calls and getting the response back to you. 这将是定期拨打电话并将响应回复给您。 As soon as the response come without containing "uninvited" string it gives an alert and also calls the post method function. 只要响应不包含“不请自来”的字符串,它就会发出警报并调用post方法函数。

This can be repeatedly called with following code. 可以使用以下代码重复调用它。

window.setInterval("checkIfIAmInvited()", 5000);

This will check every 5 seconds if there are any invitations. 这将每5秒检查一次是否有邀请。 Alternatively for a more better control jquery plugins like Timer can be used to execute something repeatedly. 另外,为了更好地控制控件,可以使用诸如Timer之类的jquery插件重复执行某些操作。

Using (at least) two database trips is a poor way to handle a real time chat. 使用(至少)两次数据库旅行是处理实时聊天的一种不好的方法。 If you were doing some stored chat aka msging system it's fine. 如果您正在做一些存储的聊天aka信息系统,那就很好。 But considering you only need to hold on to the text for a second or two, I would either use SQL-Lite or another in memory solution only. 但是考虑到您只需要保留文本一两秒钟,我将只使用SQL-Lite或另一个内存解决方案。 It would be much faster and easier on your server. 这将在您的服务器上更快,更轻松。

For chat and instant messaging systems, you should investigate the use of a Push-based technology such as Comet. 对于聊天和即时消息系统,您应该研究使用基于推的技术(例如Comet)。 More info in this thread : an implementation with jquery. 该线程中的更多信息:jquery的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM