PHP:Stomp - 是否可以捕获 "send()"上的错误?

标签 php stomp

我正在使用PHP Stomp client发送践踏消息。

我想在后台保持持久连接打开,并偶尔发送消息。

但是,如果在打开连接(在 send() 上)后发生连接错误,我找不到处理连接错误的方法。

例如,运行时:

<?php
$stomp = new Stomp('tcp://localhost:61613');

sleep(5); // Connection goes down in the meantime

$result = $stomp->send('/topic/test', 'TEST');

print "send " . ($result ? "successful\n": "failed\n");
?>

输出:发送成功

即使连接在 sleep() 期间断开,send() 也始终返回 true。

docs不是很有帮助,Stomp::error()stomp_connect_error() 也没有多大帮助,因为它们返回 false

作为临时解决方案,我在每次 send() 之前重新连接。

有更好的方法来捕获连接错误吗?

最佳答案

specification 中找到答案stomp 协议(protocol)本身:

Any client frame other than CONNECT MAY specify a receipt header with an arbitrary value. This will cause the server to acknowledge receipt of the frame with a RECEIPT frame which contains the value of this header as the value of the receipt-id header in the RECEIPT frame.

因此,设置“receipt” header 会使请求同步,因此与服务器的连接必须处于事件状态。

所以代码:

$result = $stomp->send('/topic/test', 'TEST');
print "send " . ($result ? "successful\n": "failed\n");

$result = $stomp->send('/topic/test', 'TEST', array('receipt' => 'message-123'));
print "send " . ($result ? "successful\n": "failed\n");

给出输出:

send successful
send failed

这似乎不是这种情况下的最佳解决方案,但它对我有用。

如果有人知道更好的方法,我会很高兴听到。


更新:

最终我切换到了Stomp-PHP (纯 PHP 客户端)而不是处理它的 Pecl stomp 客户端 much better .

关于PHP:Stomp - 是否可以捕获 "send()"上的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10570485/

相关文章:

php - 无法在 Laravel 中使用自定义 PHP 插件

php - PHPmail 函数中的 “Could Not Access File:”

java - 更改 Spring/Stomp/Websocket/Messaging 中的 session ID

Spring STOMP 不完整框架

php - 从 SQL 循环遍历(并显示)多个表行

javascript - 淡入动画

php - 循环遍历MySQL数据库直到有结果,然后中断

java - 用户连接时获取 native header

spring - Websockets - 带有 Spring Boot 和 STOMP 的 CSRF

spring - 如何在 Spring 4 STOMP over WebSocket 配置中回复未经身份验证的用户?