我正在使用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/