服务器如何同时与两个客户端通信(JavaScript、HTML、PHP)?
我有一个作业要做,为此我可以使用任何 www 技术,如 HTML、JavaScript、PHP 等。我很遗憾地说我没有研究过任何这些技术。因此,我学习了一些教程并浏览它们以寻找答案。
我找到了许多问题的解决方案,但有一个问题尚未解决。是这样的:
我希望两个客户端通过服务器进行通信以完成此任务。一个发送消息,服务器处理它并将其转发给下一个。
无论如何,PHP 教程都没有向我展示如何执行此操作。他们都谈到了客户端与服务器之间的通信。
请帮忙。告诉我一种方法来做到这一点。谢谢。
I got an assignment to do and for that I could use any www technology like HTML, JavaScript, PHP etc. I'm really sorry to say that I haven't studied any of these technologies. Therefore I took few tutorials and skimmed through them searching for answers.
I found solutions for many problems but one problem yet unsolved. It is this:
I want two clients to communicate through a server for this assignment. One send a message, server processes it and forwards it to the next.
None of PHP tutorials showed me anyway of doing this. All of them talked of communication between one client with a server.
Please help. Show me a way to do this. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,如果不恢复到尖端(可能是黑客/不可靠)技术,您的 PHP 服务器无法启动与您已加载到 Web 浏览器中的页面的通信。这是 HTTP 协议工作方式的结果。
解决此问题的一种方法是在“接收”端轮询数据。类似于发布-订阅模式。
实现此目的的一种方法是:
我建议您看一下:
http://en.wikipedia .org/wiki/Publish/subscribe
另外,要获得最先进的方法,请查看 Socket.IO:
href="http://socket.io/" rel="nofollow" title ="Socket.IO">http://socket.io
Currently, without reverting to cutting-edge (and possibly hacky/unreliable) techniques, your PHP server cannot initiate communications with a page you've already loaded into a web browser. This is a result of the way the HTTP protocol works.
One way to solve this would be polling on the "receiving" end for data. Something like a publish-subscribe pattern.
One way to do this would be:
I would suggest you take a look at:
http://en.wikipedia.org/wiki/Publish/subscribe
And also, for a cutting edge way to do this, check out Socket.IO:
http://socket.io
您可能想通过 Google 搜索“php 聊天服务器”。构建聊天服务器是一种简单的入门方法。
http:// net.tutsplus.com/tutorials/javascript-ajax/how-to-create-a-simple-web-based-chat-application/
http://code.jenseng.com/jenChat/
You might want to Google on "php chat server." Building a chat server is a simple way to get started.
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-create-a-simple-web-based-chat-application/
http://code.jenseng.com/jenChat/