2

我正在使用PHP Stomp 客户端发送 stomp 消息。

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

但是,如果在打开连接后(在 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");
?>

输出:send successful

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

文档不是很有帮助,Stomp::error()并且在stomp_connect_error()返回时也没有太大帮助false

作为临时解决方案,我在每个send().

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

4

1 回答 1

2

在 stomp 协议本身的规范中找到了答案:

除 CONNECT 之外的任何客户端框架都可以指定具有任意值的接收头。这将导致服务器确认接收到带有 RECEIPT 帧的帧,该帧包含此标头的值作为 RECEIPT 帧中的接收 ID 标头的值。

因此设置“receipt”标头会使请求同步,因此与服务器的连接必须是活动的。

所以代码:

$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 客户端,它处理得更好

于 2012-05-15T15:47:32.133 回答