我是 Ratchet 和 Symfony 的新手。我正在努力熟悉它们。我使用了 session 的替代方法,这很奇怪但有效。这是它:
在 JS(原生 PHP session )中
name = "<?php echo $_SESSION['name']; ?>";
msg = elem.value;
socket.send(name+"|"+msg);
发送的参数是
explode
带分隔符 |
.它创建了两个字符串。一个是名称,另一个是 msg,然后将其发送到除发件人之外的所有连接。list($name, $message) = explode("|", $msg);
我如何使用 Symfony Session 在用户输入时保存昵称,并且可以通过 MyApp 命名空间中的 Chat 类访问它?
我有 API 文档,但没有使用 session 的简单示例。
API Documentation
.树:
/www
|
|___/src
| |
| |____/MyApp/
| |
| |___Chat.php // The Chat Class
|
|___/bin
| |
| |___chat-server.php
|
|___/vendor
|
|___/chat.php // <-- Chat Room, sending and receiving here
|
|___/index.php // <-- Ask for nickname here
|
|___/composer.phar
|
|___/composer.lock
|
|___/main.js // <-- Index HTML javascript
|
|___/socket.js // <-- Websocket handler for char.php
|
|___/main.css // <-- All style
最佳答案
Controller 可以在 session 中设置昵称:
/**
* @Route("/hello/{nickname}")
*/
public function indexAction($nickname)
{
$session = $this->getRequest()->getSession();
$session->set('nickname', $nickname);
}
要在您的聊天应用中检索昵称,
public function onOpen(ConnectionInterface $conn)
{
$nickname = $conn->Session->get('nickname');
}
关于php - Ratchet session 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18671031/