简体   繁体   English

浏览器之间通过PHP进行通信

[英]Communication between browsers via PHP

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: 我正在为一个学校项目构建一个简单的ajax聊天客户端,并想到了实现这一点的方法,但似乎IMO 非常繁琐的方法:

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. 1)用户A发送服务器端PHP脚本接受并保存到数据库的消息2)用户B的浏览器定期启动服务器端PHP脚本,检查数据库中是否有用户B的消息.PHP脚本从用户A查找消息并返回它们。

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应用程序......如果我在没有浏览器+ HTTP的情况下进行此操作,我只需要创建一个带有持久类的Java程序,该类可以监听TCP套接字,并将消息转发到相应的地址)

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. 首先尝试使用您的方法,然后再进行此操作。

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. 每个PHP“实例”仅持续请求的持续时间,因此您需要持久存储(例如聊天消息的数据库)。

And yes, I know that this does work because I've implemented a very similar system in the past. 是的,我知道这确实有效,因为我过去实现了一个非常相似的系统。

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. Teehoo,如果你想要一种工作方法,你所建议的就好了,特别是如果它只适用于学校项目。

If you want something like the way that Facebook does it, you should look at commet HTTP connections. 如果你想要像Facebook那样的方式,你应该看看commet HTTP连接。 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. 它通过不断轮询新消息来提供快速更新并几乎消除了AJAX依赖性,因为您始终保持与Web服务器的连接不断打开。

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). 读一下彗星http连接,(但不要看彗星聊天应用程序,他们是一家公司试图销售类似于facebook聊天应用程序的产品而不是你想要的......虽然他们已经实现了彗星方法)。

Comet Chat # 彗星聊天#

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

Then read this: 然后读这个:

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

But anyway, what you suggested is fine for a school project. 但无论如何,你建议对学校项目没问题。

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. 你只需要使用javascript / ajax轮询数据库中的新消息。

If you are interested in not using a database i'd suggest using a non-sql approach. 如果您对不使用数据库感兴趣,我建议使用非SQL方法。 Google is your friend on this as there are many options. 谷歌是你的朋友,因为有很多选择。

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 eg memcache. 您还可以考虑通过共享内存存储,例如memcache。

What you can also do is use a comet-like javascript approach. 您还可以使用类似彗星的JavaScript方法。 You keep a connection to a PHP page open from the browser until the PHP page receives a message. 您保持与浏览器打开的PHP页面的连接,直到PHP页面收到消息。

However you are still limited to serving the PHP page per connection so you need some storage. 但是,您仍然只能为每个连接提供PHP页面,因此您需要一些存储空间。 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 在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 Comet聊天方法: http//www.zeitoun.net/articles/comet_and_php/start#comet_with_classic_ajax_litte_chat_demo

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

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