通过 PHP 实现浏览器之间的通信

发布于 2024-08-22 08:15:48 字数 313 浏览 4 评论 0原文

我正在为一个学校项目构建一个简单的 ajax 聊天客户端,并想到了一种实现此目的的方法,但在我看来,这种方法非常麻烦:

1)用户 A 发送消息,该消息被服务器端 PHP 脚本并保存到数据库 2)用户B的浏览器定期启动服务器端PHP脚本来检查数据库中是否有用户B的消息。PHP脚本找到来自用户A的消息并将其返回。

这是正确的方法吗?没有数据库可以实现这两个用户之间的通信吗?

(这是我的第一个 Web 应用程序...如果我在没有浏览器 + HTTP 的情况下制作它,我只需制作一个带有持久类的 Java 程序,该类侦听 TCP 套接字,并将消息转发到适当的地址)

I am building a simple ajax chat client for a school project and have thought of a way to implement this, but it seems IMO to be very cumbersome approach:

1) User A sends message which is accepted by a server-side PHP script and saved to database
2) The browser of User B periodically launches a server side PHP script to check if there are any messages in the database for User B. PHP script finds messages from User A and returns them.

Is this the right approach? Can communication between these two users be achieved without a database?

(This is my first web-application...if I was making this without browsers + HTTP, I would just make a Java program with persistent class that listened on TCP sockets, and forwarded messages to the appropriate address)

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

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

发布评论

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

评论(6

美胚控场 2024-08-29 08:15:48

是的,您的解决方案足以开始使用。您正在做的是轮询服务器是否有特定用户的任何聊天消息。足够好了。

但是,如果您想进入下一个级别(可能会很困难),您可以拥有一个可以将新消息推送到客户端浏览器的服务器。这就是所谓的“彗星”。但它需要大量的服务器资源(如果您的用户群将超过数千)。

首先尝试你的方法,然后再继续。

Yes your solution is good enough for starting. What you are doing is polling the server if there are any chat messages for a particular user. Good enough.

But if you wanna goto the next level, (probably could be tough), you can have a server which can push new messages onto client browsers. This is called "Comet". But it will need extensive server resources (if your userbase is going to exceed to thousands).

Try your method first and go for this next.

删除→记忆 2024-08-29 08:15:48

每个 PHP“实例”仅在请求期间持续,因此您需要一个持久存储,例如用于聊天消息的数据库。

是的,我知道这确实有效,因为我过去已经实现了一个非常相似的系统。

Each PHP "instance" only lasts for the duration of the request, so you need a persistent store such as a database for the chat messages.

And yes, I know that this does work because I've implemented a very similar system in the past.

三生路 2024-08-29 08:15:48

彗星接近

Teehoo,如果你想要一种工作方法,你所建议的就可以了,特别是如果它只是为了一个学校项目。

如果您想要像 Facebook 那样的方式,您应该查看 commet HTTP 连接。它非常聪明。我记得当我第一次读到它时,我认为它很巧妙。它提供快速更新,并且通过不断轮询新消息几乎消除了对 AJAX 的依赖,因为您与 Web 服务器的连接始终保持打开状态。

阅读 comet http 连接,(但不要看 comet 聊天应用程序,他们是一家试图销售类似于 facebook 聊天应用程序的产品的公司,但这不是您想要的......尽管他们已经实现了 comet方法)。

彗星聊天
#

http://en.wikipedia.org/wiki/Comet_(programming)

然后阅读以下内容:

http://www.zeitoun.net/articles/comet_and_php/start

但无论如何,你的建议对于学校项目来说是很好的。

The Comet Approach

Teehoo, if you want a working method, what you have suggested would be fine, especially if its just for a school project.

If you want something like the way that Facebook does it, you should look at commet HTTP connections. Its very clever. I remember when I first read about it I thought it was ingenious. It provides for fast updates and almost eliminates the AJAX dependency by constantly polling for new messages because you keep your connection to the web server constantly open.

Take a read of comet http connections, (but don't look at the comet chat application, they are a company trying to sell a product similar to the facebook chat application and isn't what you want.. although they have implemented the comet method).

Comet Chat
#

http://en.wikipedia.org/wiki/Comet_(programming))

Then read this:

http://www.zeitoun.net/articles/comet_and_php/start

But anyway, what you suggested is fine for a school project.

静若繁花 2024-08-29 08:15:48

这是正确的方法吗?没有数据库可以实现这两个用户之间的通信吗?

您至少需要某种形式的存储 - 数据的持久性特征取决于您。数据库是长时间保存数据的好方法。

您还可以考虑使用共享内存存储,例如内存缓存。

Is this the right approach? Can communication between these two users be achieved without a database?

You at least need a storage of some form - the persistence characteristics of the data is up to you. A database is a good way to persist the data for an extended amount of time.

You could also consider going through a shared memory storage e.g. memcache.

旧城烟雨 2024-08-29 08:15:48

您还可以使用类似彗星的 JavaScript 方法。您保持从浏览器打开到 PHP 页面的连接,直到 PHP 页面收到消息。

但是,您仍然只能为每个连接提供 PHP 页面,因此您需要一些存储空间。如果你想让它非常快,你可以使用内存。

在 PHP 中使用内存: http://www.php.net/manual /en/function.apc-add.php

Comet 聊天方法: http ://www.zeitoun.net/articles/comet_and_php/start#comet_with_classic_ajax_litte_chat_demo

What you can also do is use a comet-like javascript approach. You keep a connection to a PHP page open from the browser until the PHP page receives a message.

However you are still limited to serving the PHP page per connection so you need some storage. If you want it to be really fast you could use memory.

Use memory in PHP: http://www.php.net/manual/en/function.apc-add.php

Comet approach to chat: http://www.zeitoun.net/articles/comet_and_php/start#comet_with_classic_ajax_litte_chat_demo

窝囊感情。 2024-08-29 08:15:48

答案是肯定的。你可以在没有数据库的情况下做到这一点。

但是..您必须以某种方式将数据存储在中央服务器上。对于聊天应用程序,理性数据库对于这种聊天类型的应用程序来说并不理想,但只有当您拥有大型站点时,它才真正相关。如果您正在为一个项目执行此操作,那么数据库足以存储聊天信息。您只需要使用 javascript/ajax 轮询数据库中的新消息。

如果您不想使用数据库,我建议您使用非 sql 方法。谷歌是你在这方面的朋友,因为有很多选择。

well yes is the answer. you can do it without a database.

But.. you will have to store the data on the central server some way. for a chat application a rational database isn't ideal for this chat type application but its only really relevant if you have a large site. if you are doing this for a project then a database would be plenty good enough to store the chat info. you just need to poll the database for new messages using javascript/ajax.

If you are interested in not using a database i'd suggest using a non-sql approach. Google is your friend on this as there are many options.

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