问题
I've set up Ratchet for websockets in PHP . It is connecting fine from my javascript client using (ws://localhost:8080) and successfully send/receive messages. But I want to pass some params like (ws://localhost:8080?param1=value). I'm not able to figure out how can I access param1 in my PHP script.
If possible in MessageComponentInterface::onOpen(ConnectionInterface $conn) method.
Or better: Can I associate those params with ConnectionInterface $conn. So that I've them for further communication.
I've followed http://socketo.me/docs/hello-world.
回答1:
As of a very recent update, you can now access this like so:
function onOpen( ConnectionInterface $conn ) {
$querystring = $conn->WebSocket->request->getQuery();
}
I actually just ran into this issue myself. Tested this and it's working perfectly.
回答2:
$conn->WebSocket->request
replaced with
$conn->httpRequest
which is a PSR-7 object
https://github.com/ratchetphp/Ratchet/blob/master/CHANGELOG.md
回答3:
In symfony 4 with php > 7.1
$conn->httpRequest->getUri()->getQuery()
This return all parameters into the query, only need parse string to extract the paremeter that needs.
来源:https://stackoverflow.com/questions/22761900/access-extra-parameters-in-ratchet-web-socket-requests