如何使用 PHP 和 JQuery 开发基于 Web 的 XMPP 聊天工具?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11037062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:38:09  来源:igfitidea点击:

How to develop a web-based XMPP chat facility using PHP and JQuery?

phpxmppwebsocketchat

提问by P3tro

I am looking to develop a website which features a chat facility between a website visitor and the website administrator.

我希望开发一个网站,该网站具有网站访问者和网站管理员之间的聊天功能。

I know the best way to do this would be using XMPP, however I have no experience using it. I am looking to implement this using PHP.

我知道最好的方法是使用 XMPP,但是我没有使用它的经验。我正在寻找使用 PHP 来实现这一点。

I've downloaded XMPPHP and I edited an example to send a message to my Google Chat client in GMail, but when I reply Google tells me the other end didn't get the message.

我已经下载了 XMPPHP 并编辑了一个示例,以在 GMail 中向我的 Google Chat 客户端发送消息,但是当我回复时,Google 告诉我另一端没有收到消息。

So far, the most informative tutorial is http://www.ibm.com/developerworks/xml/tutorials/x-realtimeXMPPtut/but I don't understand why I need to install 'Openfire' nor do I want to build the website on my local machine.

到目前为止,信息最丰富的教程是http://www.ibm.com/developerworks/xml/tutorials/x-realtimeXMPPtut/但我不明白为什么我需要安装“Openfire”,也不想建立网站在我的本地机器上。

Can somebody please tell me what I need (and more importantly, why) to set up this project so I can start to build the code for it?

有人可以告诉我我需要什么(更重要的是,为什么)来设置这个项目,以便我可以开始为它构建代码?

回答by Sixty4Bit

Judging from comments to other answers I am going tell you why, and a little what, but not give you a solution because I see a ton of solutions in the "Related" sidebar. You will have to pick the right one and by knowing "the why" you will be able to make an educated decision.

从评论到其他答案来看,我会告诉你为什么,还有一点什么,但不会给你一个解决方案,因为我在“相关”侧边栏中看到了大量的解决方案。您将必须选择正确的,通过了解“原因”,您将能够做出明智的决定。

For chat to feel right, there has to be some immediacy to the responses. A one second lag in time will be noticeable to users over time and give a sense of untimeliness. To make immediate or "real time" responses work in a browser requires a persistent connection so that when new information comes in, it immediately shows up.

为了让聊天感觉正确,回复必须有一些即时性。随着时间的推移,用户会注意到一秒的延迟,并给人一种不合时宜的感觉。要在浏览器中进行即时或“实时”响应,需要持久连接,以便在新信息进入时立即显示。

Persistent connections in browsers are difficult due to the request/response specifications of HTTP. There are specifications in work to bring persistent connections to browsers but those browsers are not ubiquitous. In the future persistent connections will be supplied by WebSocketsand SPDY, both of which are available in the latest versions of Chrome, Safari and FireFox with IE lagging a bit.

由于 HTTP 的请求/响应规范,浏览器中的持久连接很困难。有一些规范正在为浏览器带来持久连接,但这些浏览器并非无处不在。未来持久连接将由WebSocketsSPDY 提供,它们都可以在最新版本的 Chrome、Safari 和 FireFox 中使用,但 IE 有点滞后。

Another option for persistent connections is XMPP. XMPP is the protocol used for the Jabber chat client. Since it is an open source implementation it has been ported to many other uses. JavaScript libraries exist that allow you to connect a browser to an XMPP socket and listen for new messages. The method I have seen in the past is to send the messages to the web server, and then have the web server tell the XMPP server about the new message which then broadcasts the new message out to all of the users. However, this requires an XMPP server which raises the complexity of system.

持久连接的另一个选项是XMPP。XMPP 是用于 Jabber 聊天客户端的协议。由于它是一个开源实现,它已被移植到许多其他用途。JavaScript 库允许您将浏览器连接到 XMPP 套接字并侦听新消息。我过去看到的方法是将消息发送到 Web 服务器,然后让 Web 服务器告诉 XMPP 服务器有关新消息的信息,然后将新消息广播给所有用户。然而,这需要一个 XMPP 服务器,这增加了系统的复杂性。

Most users are not on the bleeding edge of browser versions so you will need to be able to handle older browsers. Most of the alternatives involve opening a long running connection to the server which responds whenever new data arrives. Here is a list of methods for simulating a persistent connection in older browsers:

大多数用户并不处于浏览器版本的最前沿,因此您需要能够处理较旧的浏览器。大多数替代方案涉及打开与服务器的长时间运行的连接,该连接在新数据到达时做出响应。以下是在旧浏览器中模拟持久连接的方法列表:

  • Adobe Flash Socket
  • ActiveX HTMLFile (IE)
  • Server-Sent Events (Opera)
  • XHR with multipart encoding
  • XHR with long-polling
  • Adobe Flash 插座
  • ActiveX HTML 文件 (IE)
  • 服务器发送的事件 (Opera)
  • 具有多部分编码的 XHR
  • 带长轮询的 XHR

These older methods, and WebSockets, are supported by a library called Juggernaut.

这些旧方法和 WebSockets 由名为Juggernaut的库支持。

UPDATEJuggernaut has been deprecated by the maintainer, for good reason: modern browsers support persistent connections out of the box (with the exception of IE of course) through a specification called Server-Sent Events (SSE). Backwards compatibility is now handled by polyfills (What is a polyfill?) and as the deprecation post notes, there are a couple of good ones to bring SSE to legacy browsers.

更新Juggernaut 已被维护者弃用,理由很充分:现代浏览器通过称为服务器发送事件 (SSE) 的规范支持开箱即用的持久连接(当然 IE 除外)。向后兼容性现在由 polyfills 处理(什么是 polyfill?),正如弃用帖子所述,有几个很好的将 SSE 引入旧浏览器。

回答by Rahul Ahuja

Instant Messaging apps are supposed to be real time. A website works on HTTP protocol which uses request/response method. One way to do it is POLLING. send a request for new pending messages for the user to the server. The server should be able to differentiate between the messages which has been sent and the ones which are yet to be delivered. this method is called Polling. Your browser is constantly asking the server to send any pending messages. But this may waste bandwidth and also drain battery ( in case the website is accessed using a smartphone ). Better option is to still use the XMPP server.

即时消息应用程序应该是实时的。网站基于使用请求/响应方法的 HTTP 协议。一种方法是轮询。向服务器发送对用户的新待处理消息的请求。服务器应该能够区分已发送的消息和尚未发送的消息。这种方法称为轮询。您的浏览器不断要求服务器发送任何待处理的消息。但这可能会浪费带宽并耗尽电池电量(以防使用智能手机访问网站)。更好的选择是仍然使用 XMPP 服务器。