简体   繁体   English

PHP,javascript,ajax - 2个用户之间的通信

[英]PHP, javascript, ajax - communication between 2 users

Good day to all. 大家好日子。

I need to do this: 我需要这样做:

When user X press a button user Y sees a photo. 当用户X按下按钮时,用户Y看到一张照片。 Before that user Y sees nothing. 在此之前,用户Y什么都看不见

I did this like this: I created a database on a mysql server and created a table with photos and flags. 我是这样做的:我在mysql服务器上创建了一个数据库,并创建了一个带有照片和标志的表。 When user X presses the button I send an ajax request and change the flag to the corresponding photo. 当用户X按下按钮时,我发送ajax请求并将标志更改为相应的照片。 User Y also sends ajax request every 1 sec to see if anything has changed. 用户Y还每1秒发送一次ajax请求以查看是否有任何更改。

Sounds ok, looks ok, works... it does but to so ok. 听起来不错,看起来不错,有效......但确实如此。 When 100000 users send 1 ajax request every 1 second the server crashes. 当100000个用户每1秒发送1个ajax请求时,服务器崩溃。 First mysql server then apache. 先是mysql服务器然后是apache。

So... my question is is there anyway to do this without so many ajax requests? 所以...我的问题是,如果没有这么多的ajax请求,有没有这样做呢? Something like send the page with a certain "thing" that tells the browser to keep the same socket and send later the image? 有点像发送带有某个“东西”的页面,告诉浏览器保持相同的套接字并稍后发送图像? Something like a stream but instead of images to be html pages/images/whatever. 像流一样的东西,而不是图像是html页面/图像/等等。

I know java applet would be a solution but I would need a php(for server side)+js(for client side) solution. 我知道java applet会是一个解决方案,但我需要一个php(用于服务器端)+ js(用于客户端)解决方案。

And yes I know that I'll probably get an "you can't" answer but it deserves a try. 是的,我知道我可能会得到一个“你不能”的答案,但值得一试。

What about WebSockets ? WebSockets怎么样?

edit. 编辑。 HTTP keep-alive might help. HTTP keep-alive可能有所帮助。

Because HTTP is stateless, you need to create a permanent or long-lasting socket connection between clients and server. 由于HTTP是无状态的,因此您需要在客户端和服务器之间创建永久或持久的套接字连接。

This is a chat application, written in PHP, AJAX and Ruby backend. 这是一个用PHP,AJAX和Ruby后端编写的聊天应用程序。

Web Chat application main page , more specifically the part about the socket server Web Chat应用程序主页 ,更具体地说是关于套接字服务器部分

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

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