首页 > 解决方案 > PHP中的websocket是否存在踩踏?

问题描述

我使用 Node.js 在 WebSocket 上运行 STOMP,因为它支持自定义标头。当 HTTP 握手时,服务器需要一个 cookie 来检查身份验证,而不是地址中的令牌。

作为PHPer,我想在PHP中使用这个,但是google了半天也没找到。谁能帮忙?

标签: phphttpstomp

解决方案


STOMP 不限于 Node.js,或者 Java 等。有

关于您的具体情况,如果不了解更多详细信息,很难直接给出答案,但这里有一个来自 stomp-php 库示例帽子的示例,它设置了一个自定义标头

use Stomp\Client;
use Stomp\SimpleStomp;
use Stomp\Transport\Map;


// make a connection
$client = new Client('tcp://localhost:61613');
$stomp = new SimpleStomp($client);


// send a message to the queue
$body = array('city' => 'Belgrade', 'name' => 'Dejan');
$header = array();
$header['transformation'] = 'jms-map-json';
$mapMessage = new Map($body, $header);
$client->send('/queue/test', $mapMessage);
echo 'Sending array: ';
print_r($body);

$stomp->subscribe('/queue/test', 'transform-test', 'client', null, ['transformation' => 'jms-map-json']);
/** @var Map $msg */
$msg = $stomp->read();

// extract
if ($msg != null) {
    echo 'Received array: ';
    print_r($msg->map);
    // mark the message as received in the queue
    $stomp->ack($msg);
} else {
    echo "Failed to receive a message\n";
}

推荐阅读