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

发布于 2024-08-16 13:33:06 字数 301 浏览 12 评论 0原文

我正在用 php 创建一个聊天应用程序。

目标: 当 user1 邀请 user2 时,user2 希望收到 user1 邀请他的提醒或弹出消息。

我所做的是-

user1邀请时,消息存储在数据库中。在用户端我用来检查是否 数据库是否更新。如果数据库更新,我会显示警报。这是我在 php 和 javascript 上完成的,但并不总是有效。

这是一个好方法吗?还有其他方法吗?

I am creating a Chat application in php.

AIM:
when a user1 invites user2, user2 want to get a alert or popup message that user1 has invites him.

What i have done is -

When user1 invites, the message is stored in database. 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.

Is this a good method? Any other method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无敌元气妹 2024-08-23 13:33:06

使用Ajax定期向服务器发送请求。响应将包含用户拥有的任何邀请。响应可能采用 JSON 格式,可以在 JavaScript 中轻松评估。

Use Ajax to send request to Server periodically. Response will contain any invites that user has. Response maybe in JSON which can be easily evaluated in JavaScript.

2024-08-23 13:33:06

所有客户端都可以使用 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();
         }
     });
}

要定期调用上述代码,您可以在 jquery/javascript 中使用以下代码。这将定期拨打电话并得到答复。一旦响应不包含“uninvited”字符串,它就会发出警报,并调用 post 方法函数。

可以使用以下代码重复调用它。

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

它将每 5 秒检查一次是否有任何邀请。或者,为了更好地控制,可以使用 Timer 等 jquery 插件来重复执行某些操作。

All the clients can use jquery and send following request periodically:

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. 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.

This can be repeatedly called with following code.

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

This will check every 5 seconds if there are any invitations. Alternatively for a more better control jquery plugins like Timer can be used to execute something repeatedly.

删除会话 2024-08-23 13:33:06

使用(至少)两个数据库行程是处理实时聊天的糟糕方法。如果您正在做一些存储的聊天(又名消息系统),那就没问题了。但考虑到您只需要保留文本一两秒钟,我会使用 SQL-Lite 或仅使用其他内存解决方案。在您的服务器上它会更快更容易。

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. 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. It would be much faster and easier on your server.

弃爱 2024-08-23 13:33:06

对于聊天和即时消息系统,您应该研究基于推送的技术(例如 Comet)的使用。更多信息请参见此线程:使用 jquery 的实现。

For chat and instant messaging systems, you should investigate the use of a Push-based technology such as Comet. More info in this thread: an implementation with jquery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文