您的位置:首页技术文章
文章详情页

请问大神,以下心跳监听部分代码怎么调用?谢谢!

【字号: 日期:2022-06-02 18:11:36浏览:69作者:猪猪

问题描述

以下是web服务器后台,通过“php XXX.php“命令,使其时刻保持运行的心跳监听代码片断。我怎样将try里面的$A显示在前端浏览器上?谢谢!

while (true) {

if ($stomp == null || !$stomp->getClient()->isConnected()) {

echo "connection not exists, will reconnect after 10s.", PHP_EOL;

sleep(10);

$stomp = start_consume();

}

try {

//throw new Exception("自定义错误");

//处理消息业务逻辑。

$A = $stomp->read();

echo "start consumer:";

echo "<br/>";

// echo $A;

$arr = explode(':',$A);

// echo "<br/>";

// $B = json_decode($A,true);

$arr1 = ($arr[16]);

// var_dump ( $arr1[0]);

echo "<br/>";

$B = json_encode($arr1,true);

echo $B;

// $B1 = json_decode($B,true);

echo "<br/>";

}

catch(HeartbeatException $e) {

echo 'The server failed to send us heartbeats within the defined interval.', PHP_EOL;

$stomp->getClient()->disconnect();

} catch(Exception $e) {

echo 'process message occurs error '. $e->getMessage() , PHP_EOL;

}

问题解答

回答1:

你这个是php代码,心跳监听 你用前端一直请求这个方法,$A 时传给前端,你是如何给前端传值的,就怎么传。

一般是前端用ajax请求php,php用echo json_encode 返回给ajax的

相关文章: