强制使用 JAXL/XMPPHP 制作的聊天机器人在断开连接时重新连接

发布于 2024-12-29 13:46:28 字数 267 浏览 1 评论 0原文

我正在使用 JAXL 库来实现用 php 编写的 jabber 聊天机器人,然后使用 PHP CLI 作为后台进程运行。

一切工作得很好,但我一直很难弄清楚如何让聊天机器人在断开连接时重新连接!

我注意到,当我让它运行整晚时,有时它会脱落并且不会再出现。我在 jaxl_post_disconnect 钩子之后尝试过 $jaxl->connect() 和 $jaxl->startStream() 以及 $jaxl->startCore() ,但我认为我错过了一些东西。

I'm using the JAXL library to implement a jabber chat bot written in php, which is then ran as a background process using the PHP CLI.

Things work quite well, but I've been having a hard time figuring out how to make the chat bot reconnect upon disconnection!

I notice when I leave it running over night sometimes it drops off and doesn't come back. I've experimented with $jaxl->connect() and $jaxl->startStream(), and $jaxl->startCore() after jaxl_post_disconnect hook, but I think I'm missing something.

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

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

发布评论

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

评论(3

凹づ凸ル 2025-01-05 13:46:28

一种解决方案是测试您的连接:

1) making a "ping" request to your page/controller or whatever
2) setTimeout(functionAjaxPing(), 10000);
3) then read the Ajax response and if == "anyStringKey" then your connection works find
4) else: reconnect() / errorMessage() / whatEver()

我认为这就是 IRC 聊天所使用的。

但这会产生更多流量,因为需要 ping/ping 请求。

希望这会对您有所帮助。 :)

One solution would be to test your connection:

1) making a "ping" request to your page/controller or whatever
2) setTimeout(functionAjaxPing(), 10000);
3) then read the Ajax response and if == "anyStringKey" then your connection works find
4) else: reconnect() / errorMessage() / whatEver()

This is what IRC chat use i think.

But this will generate more traffic since the ping/ping request will be needed.

Hop this will help you a bit. :)

抠脚大汉 2025-01-05 13:46:28

如果您使用的是 Jaxl v3.x,则只需为 on_disconnect 添加回调 事件

此外,您还必须使用 XEP-0199 XMPP Ping。该 XEP 的作用是定期向连接的 jabber 服务器发送 XMPP ping。它还会接收服务器 ping 并发送回所需的 pong 数据包(例如,如果您的客户端没有回复服务器 ping,jabber.org 将在一段时间后断开您的连接)。

最后,您还必须使用空白 ping。空白 ping 是发送到服务器的单个空格字符。这通常足以使 NAT 设备认为连接“活动”,对于某些 Jabber 服务器(例如 Openfire)也是如此。它还可以使操作系统更快地检测丢失的连接 - 没有发送或接收数据的 TCP 连接与丢失的连接无法区分。

If you are using Jaxl v3.x all you need is to add a callback for on_disconnect event.

Also you must be using XEP-0199 XMPP Ping. What this XEP will do is, periodically send out XMPP pings to connected jabber server. It will also receive server pings and send back required pong packet (for instance if your client is not replying to server pings, jabber.org will drop your connection after some time).

Finally you MUST also use whitespace pings. A whitespace ping is a single space character sent to the server. This is often enough to make NAT devices consider the connection “alive”, and likewise for certain Jabber servers, e.g. Openfire. It may also make the OS detect a lost connection faster—a TCP connection on which no data is sent or received is indistinguishable from a lost connection.

做个少女永远怀春 2025-01-05 13:46:28

我最终做的是创建一个 crontab 来简单地再次执行 PHP 脚本。

在 PHP 脚本中,我读取了最后一个 fork 的 pid 的特定文件。如果它存在,脚本会尝试杀死它。然后该脚本使用 pcntl_fork() 分叉进程(无论如何,这对于守护 PHP 脚本很有用)并将新的 PID 捕获到文件中。然后,fork 像往常一样使用 JAXL 登录到 Jabber。

在与 JAXL 的作者交谈后,很明显这将是实现这一目标的最简单方法,尽管很hacky。然而,作者可能在最近的迭代中已经解决了这个特定的缺陷。

这种特定方法的一个缺陷是它需要 pcntl_fork(),默认情况下它不是用 PHP 编译的。

What I ended up doing was creating a crontab that simply executed the PHP script again.

In the PHP script I read a specific file for the pid of the last fork. If it exists, the script attempts to kill it. Then the script uses pcntl_fork() to fork the process (which is useful for daemonifying a PHP script anyway) and capture the new PID to a file. The fork then logs in with to Jabber with JAXL per usual.

After talking with the author of JAXL it became apparent this would be the easiest way to go about this, despite being hacky. The author may have worked on this particular flaw in more recent iterations, however.

One flaw to this particular method is it requires pcntl_fork() which is not compiled with PHP by default.

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