简体   繁体   English

PHP中是否有踩过websocket?

[英]Is there stomp over websocket in PHP?

I use Node.js to run STOMP over WebSocket because it supports custom headers.我使用 Node.js 在 WebSocket 上运行 STOMP,因为它支持自定义标头。 When HTTP handshake, the server needs a cookie to check auth, not the token in the address.当 HTTP 握手时,服务器需要一个 cookie 来检查身份验证,而不是地址中的令牌。

As PHPer, I want to use this in PHP, but search google for a long time and found no way.作为PHPer,想在PHP中使用这个,但是google了半天也没找到办法。 can anyone help?谁能帮忙?

STOMP is not limited to Node.js, or Java, etc. There are STOMP不限于Node.js,或者Java等,还有

Regarding your specific scenario, it's hard to come up with a straight-up answer without knowing more details but here is an example from the stomp-php library samples hat sets a custom header:关于您的具体情况,在不了解更多细节的情况下很难直接给出答案,但这里有一个来自 stomp-php 库示例帽子的示例,其中设置了自定义 header:

 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"; }

you need a websocket stream wrapper if you want to connect using a protocol如果要使用协议进行连接,则需要 websocket stream 包装器

You can use php-stomp-frame你可以使用 php-stomp-frame

https://github.com/jeeinn/php-stomp-frame https://github.com/jeenn/php-stomp-frame

composer require jeeinn/php-stomp-frame作曲家需要 jeeinn/php-stomp-frame

// over wss
require __DIR__ . '/vendor/autoload.php';

$url = 'wss://echo.websocket.org:443';
$userName = 'test';
$password = 'passcode';
$queue = 'service_queue_v1.2';

$stompFrame = new \Stomp\Frame();
# connect frame
$connectFrame = $stompFrame->setLogin($userName, $password)->setHeartBeat(0, 10000)->getConnect();
# subscribe frame
$subscribeFrame = $stompFrame->getSubscribe($queue);

#use websocket
$client = new \WebSocket\Client($url);
$client->text($connectFrame);
//var_dump($client->isConnected());
$client->text($subscribeFrame);

# loop listening
while (true) {
    try {
        $message = $client->receive();
        $parsed = $stompFrame->parser($message);
        //print_r($parsed);
        # Error, Break while loop to stop listening, Possibly log errors
        if ($parsed['command'] == 'ERROR') {
            echo $parsed['body'];
            $client->close();
            break;
        }
        // Deal your data
        $data = json_decode($parsed['body'], true);
        print_r($data);
        // Act[enter image description here][4] on received message
        // Later, Break while loop to stop listening
    } catch (Exception $e) {
        // Possibly log errors
        echo $e->getMessage();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM