Search code examples
phpwebsocketratchet

Ratchet - Could not get query string on server side


On client side:

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

On server side

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

...
}

Here is the dump result what I got from my console

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) {
  }
}

I can't see any clue about the value of two query strings on my URL id and tid. Updated: I have tried to dump all in the variable $request on Http/Route to check, I can see the getQuery works, but how could I do same thing on the function 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) {
       ...
     }
   }
 }

The message is transferred correctly between the client and server but the query strings. Any help is appreciated.


Solution

  • I don't really comprehend the following code in src\Ratchet\Http then I don't make sure whether it is a bug or not.

    $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);
    

    But I have modified as following:

    $parameters = $request->getQuery();
    

    My above line will override the iteration to set something into the $params. At least I can access to the query strings