PHP Jabber:如果我登录并检查消息并断开连接,在其他用户端我将显示为已断开连接

发布于 2024-07-19 06:20:59 字数 363 浏览 7 评论 0原文

我不确定我所做的是否绝对正确。 但这里是这样的:

  1. 用户通过基于 Web 的界面登录聊天
  2. 用户通过 Comet 获悉更新 用户
  3. 输入详细信息,该详细信息转到 PHP 文件,该文件进一步连接到 Jabber 服务器

现在的问题是,当用户想要发送消息时,它是简单,运行 php,在其中连接到 jabber 服务器并发送消息。 当我等待消息时,问题出现了。 因为如果我登录并检查消息并断开连接,在其他用户端我将显示为已断开连接。

我是否以错误的方式处理这个问题? 我应该直接连接到 Jabber 服务器(通过 javascript)而不是中间的 PHP 层吗? 如何通过PHP接收消息?

Am not sure if what I am doing is absolutely correct. But here goes:

  1. User logins into chat via web-based interface
  2. User is informed of updates via Comet
  3. User enters details which goto a PHP file which further connects to a Jabber server

Now the problem is that when the user wants to send a message, it's simple, run php in which i connect to jabber server and send the message. The problem arises when I am waiting for a message. Cause if I login and check messages and disconnect, on the other users end I will show up as disconnected.

Am I approaching this problem in a wrong way? Should I directly connect to the Jabber server (via javascript) instead of a PHP layer in between? How to recieve messages via PHP?

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

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

发布评论

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

评论(3

与君绝 2024-07-26 06:20:59

我还没有尝试过,但您可能想看看 xmpphp。 其次,您可能需要考虑让用户在登录您的网站时保持登录 XMPP 服务器(也称为 Jabber 服务器)。 您可能希望有某种超时,以防他们离开您的网站并且不再回来。

至于是否应该通过 JavaScript 连接,我不明白为什么不能。 我建议您选择对您来说最简单的方式。 对于这种情况,您可能需要查看 Strope,我听说过关于它的好消息。

我广泛使用的唯一 XMPP 库是 headstock,但这需要使用 python卡梅利亚

I haven't tried it out, but you might want to look at xmpphp. Secondly, you might want to consider keeping the user logged in to the XMPP server (aka a Jabber server) for as long as they're logged in to your website. You probably want to have a timeout of some kind in case they leave your website and don't come back.

As for whether or not you should connect via JavaScript, I don't see why you couldn't. I would suggest that you go for whatever seems the simplest to you. You might want to check out Strophe, which I hear good things about, for that case.

The only XMPP library that I've used extensively though is headstock, but that requires using python and Kamaelia.

七堇年 2024-07-26 06:20:59

这是 http 的固有问题(或功能) - 没有持久的连接(不是真的)。 你需要一个解决方法,没有真正的解决方案。

你可以用java或flash来做到这一点,但这并不是很好(javascript 4tw!)。

另一种可能性是创建一个中间客户端,将浏览器和网络服务器之间的连接转换为网络服务器和 jabber 服务器之间的连接。 很混乱,但有可能。

或者也许有一个 API 可以帮助解决这个问题。

通过javascript直接连接到jabber服务器
我可能睡过了最新的ajax发明,但据我所知,你只能与源html文件来自的主机进行通信(忽略greasmonkey和插件)。 没有不同的域,没有不同的端口,时期。除非您要教您的 jabber 服务器如何向浏览器提供 chatpage-html,否则这会出现问题。 此外,保持联系甚至不起作用,因为这需要多部分响应。 这些仅受 mozilla 支持,这就是丑小鸭 COMET 存在的原因。 comet 本身就是一种解决方法,以避免在传输数据时无法保持连接。

this is an inherent problem (or feature) with http - there are no lasting connections (not really). you need a workaround, there is no real solution.

you could do it with java or flash, but that's not really nice (javascript 4tw!).

the other possibility would be to create an intermediate client what translates connections between the browser and the webserver to connections between the webserver and the jabber server. messy, but possible.

or maybe there is an API that helps with this.

directly connecting to the jabber server via javascript
i possibly slept through the latest ajax-inventions, but afaik you can only communicate with the host the source-html file comes from (ignoring greasmonkey and addons). no different domains, no different ports, period. unless you're going to teach your jabber server how to serve your chatpage-html to the browser, this will get problematic. moreover, staying connected doesn't even work, because that would require multipart-responses. those are only supported by mozilla, and this is why the ugly duckling COMET even exists in the first place. comet itself is a workaround to avoid the inability to hold connections while transfering data.

最偏执的依靠 2024-07-26 06:20:59

据我所知,问题在于另一端的 Jabber 用户何时响应。 那里的问题,至少部分是,用户正在响应 Jabber 服务器上的另一个用户,但您希望 php 脚本知道该响应已经发生,而无需保持连接打开(这是有道理的,因为脚本可能不再运行)。

一种选择(尽管非常愚蠢)是:

  • 有一个可以代理与 Jabber 服务器的连接的 php 脚本,以便为页面上的用户发送和接收,

  • 使用 AJAX 为用户发送消息(AJAX 将指向上述脚本,该脚本将发送消息。)

  • 有一个 Javascript 无限循环,每 10 秒左右 ping 相同的脚本一次,检查以查看如果有消息。 如果有,它们将被传回客户端并输出给用户。

上述只有两个问题:

1)如果消息传输时用户没有连接,php脚本是否仍然会看到/获取消息?

2) 每 3 秒发出一次 ajax 请求的客户端循环可能会消耗巨大的资源。

解决方案 2:

OpenFire jabber 服务器。 它带有内置的网络聊天客户端,并且有一个名为 Fastpath 的插件,旨在处理客户端上基于 HTML 的聊天(例如太多支持页面上的“立即与代理聊天!”功能。)

我们在工作中使用它,它是非常可定制的,可以与其他脚本集成(例如,如果您想要一个脚本从登录中填写用户详细信息,或添加一些自定义头像,或其他什么),并且它(OpenFire )有大量其他扩展和插件,如果这不是您想要的,它们可能有您正在寻找的东西。

So the issue, as far as I can tell, is that when the Jabber user on the other end responds. The problem there, at least in part, is that the user is responding to another user on the Jabber server, yet you want the php script to be aware that this response has taken place without holding the connection open (which makes sense since the script is no longer running, probably).

One option, albeit a really silly one, is:

  • Have a php script that can broker a connection to the Jabber server for both sending and receiving for the user on your page,

  • Use AJAX to send messages for the user (the AJAX would point to the above script, the script would send the message.)

  • Have a Javascript infinite loop that pings the same script ever 10 seconds or so, checking in to see if there are messages. If there are, they get passed back to the client and output to the user.

There are only two issues with the above:

1) If the user isn't connected when the message is transmitted, will the php script still see/get the message?

2) A client side loop that makes ajax requests every 3 seconds would probably be a huge drain.

Solution 2:

OpenFire jabber server. It comes with a web chat client built in, and it has an addon called Fastpath, which is meant to handle HTML-based chats on the client end (like the "chat with an agent now!" feature on too many support pages.)

We use this at work and it is very customizable, can be integrated with other scripts (for instance, if you want a script that fills in the user details from their login, or adds some custom avatar, or whatever), and it (OpenFire) has tons of other extensions and addons that, if this isn't what you want, they probably have what you are looking for.

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