php - Ratchet - 无法在服务器端获取查询字符串

标签 php websocket ratchet

在客户端:

var ws = new WebSocket('ws://localhost:8082/chat?id=123&tid=7');

在服务器端
class MyApp implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {

        var_dump($conn->WebSocket->request->getQuery()); // I tried to access to query string here, but it did not work
        $this->clients->attach($conn);
    }

...
}

这是我从控制台得到的转储结果
class Guzzle\Http\QueryString#85 (5) {
  protected $fieldSeparator =>
  string(1) "&"
  protected $valueSeparator =>
  string(1) "="
  protected $urlEncode =>
  string(8) "RFC 3986"
  protected $aggregator =>
  NULL
  protected $data =>
  array(0) {
  }
}

我在我的 URL id 上看不到任何关于两个查询字符串值的线索和 tid .
更新:我试图将所有内容转储到变量 $request 中在 Http/Route 上检查,我可以看到 getQuery 工作,但是我怎么能在 onOpen 函数上做同样的事情?
 protected $url =>
 class Guzzle\Http\Url#67 (8) {
   protected $scheme =>
   string(4) "http"
   protected $host =>
   string(9) "localhost"
   protected $port =>
   int(8082)
   protected $username =>
   NULL
   protected $password =>
   NULL
   protected $path =>
   string(5) "/chat"
   protected $fragment =>
   NULL
   protected $query =>
   class Guzzle\Http\QueryString#66 (5) {
     protected $fieldSeparator =>
     string(1) "&"
     protected $valueSeparator =>
     string(1) "="
     protected $urlEncode =>
     string(8) "RFC 3986"
     protected $aggregator =>
     NULL
     protected $data =>
     array(3) {
       ...
     }
   }
 }

消息在客户端和服务器之间正确传输,但查询字符串。
任何帮助表示赞赏。

最佳答案

我真的不理解 src\Ratchet\Http 中的以下代码,然后我不确定它是否是一个错误。

$parameters = array();

        foreach($route as $key => $value) {
            if ((is_string($key)) && ('_' !== substr($key, 0, 1))) {
                $parameters[$key] = $value;
            }
        }

 $url = Url::factory($request->getPath());
        $url->setQuery($parameters);

但我已经修改如下:
$parameters = $request->getQuery();

我上面的行将覆盖迭代以将某些内容设置到 $params 中.至少我可以访问查询字符串

关于php - Ratchet - 无法在服务器端获取查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25503439/

相关文章:

php - 在 PHP 上使用 Ratchet 和 WebSockets 进行私有(private)和群聊

php - 无法使用php和Mysql更新数据库

c# - 使用 Nethereum 订阅合约事件

python - 使用 Autobahn/Twisted 在 Tornado HTTP Handler 中建立 websocket 连接

php - 关闭 Websockets 连接

php - 如何在 Alpine Linux 容器上安装 ZeroMQ for PHP?

php - 如何在 wordpress 中使用 get_post() 将 get_template_part() 用于特定帖子?

PHP - 使用登录系统保护仅限成员(member)的页面

php - 拉维尔 : Your requirements could not be resolved to an installable set of packages

node.js - 在服务器中监听客户端发出的套接字