Access extra parameters in Ratchet web socket requests

你。 提交于 2019-12-04 18:21:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!