踩踏订阅无响应

Stomp Subscribe No Response

我想订阅Spring框架WebSocket并收到回复

根据我的目标 WebSocket 服务器,通信是使用 STOMP 发布协议完成的(基于 Java Spring 框架 API 构建)https://stomp.github.io/stomp-specification-1.1.html

现在我正在使用的客户端是基于 PHP 构建的,并使用 https://github.com/Textalk/websocket-php/ 作为 websocket 客户端。

我接收服务器响应的想法是根据这个人的回答遵循 STOMP over Websocket 技术

使用当前的 websocket 客户端,我执行这些步骤

  1. 发送连接(请求?)

  2. 发送订阅

  3. 主动接收回复

     $client = new WebSocket\Client($ws_url);
     //Step 1 Inintate connection;
     $open_msg = "CONNECT\naccept-version:1.0,1.1,2.0\n\n\x00\n";
     //Step 2 Subscribe Request;
     $client->send($open_msg);
     $subs = "SUBSCRIBE\nid:0\ndestination:/user/queue\nack:auto\n\n\x00\n";
     $client->send($subs);
     while (true) {
           try {
               $message = $client->receive();
               echo $message;
               // Act[enter image description here][4] on received message
               // Later, Break while loop to stop listening
           } catch (\WebSocket\ConnectionException $e) {
               // Possibly log errors
           }
         }
     $client->close();
    

连接(第 1 步)已完成并经过测试。

current send and receive result image

因为它在循环中 运行,所以总是打印 Received

有谁知道为什么 API 没有发送回复?

事实证明,我必须改为实现其他 Websocket 库

而不是使用 https://github.com/Textalk/websocket-php/ , I moved on and use https://github.com/ratchetphp/Pawl

我不知道刚刚发生了什么。不过我觉得Textalk是同步websocket库,ratchet是异步websocket库

我目前的假设是每当你想在 websocket 上做 Stomp 时,确保

  1. 发送连接消息("CONNECT\naccept-version:1.0,1.1,2.0\n\n\x00\n")
  2. 发送订阅("SUBSCRIBE\nid:0\ndestination:/user/queue\nack:auto\n\n\x00\n")
  3. 使用异步 Websocket 而不是同步 Websocket

祝你有愉快的一天