简体   繁体   English

棘轮会话存储

[英]Ratchet Session storage

I am newbie to Ratchet and Symfony both. 我是Ratchet和Symfony的新手。 I am trying to get familiar with them. 我想要熟悉它们。 I have used alternative to sessions which is quite weird but works. 我已经使用了替代会话,这很奇怪但是很有效。 Here's it: 这是它:

In JS (a native PHP session) 在JS(本机PHP会话)中

name = "<?php echo $_SESSION['name']; ?>";
msg = elem.value;
socket.send(name+"|"+msg);

The sent argument is then explode with delimiter | 然后使用分隔符| explode发送的参数 . It creates two strings. 它创建了两个字符串。 One is the name and other is msg which is then sent to all connections except the sender. 一个是名称,另一个是msg,然后发送到除发件人之外的所有连接。

list($name, $message) = explode("|", $msg);

How can I use Symfony Session which saves nickname when user enters it and it will be accessible by my Chat class in MyApp namespace? 我如何使用Symfony Session在用户输入时保存昵称,并且MyApp命名空间中的Chat类可以访问它?

I have API docs but there is no simple example for using session. 我有API文档,但没有使用会话的简单示例。 API Documentation . API Documentation

Tree: 树:

/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

A controller can set the nickname to in the session: 控制器可以在会话中设置昵称:

/**
 * @Route("/hello/{nickname}")
 */
public function indexAction($nickname)
{
    $session = $this->getRequest()->getSession();
    $session->set('nickname', $nickname);
}

To retrieve the nickname in your Chat app, 要在聊天应用中检索昵称,

public function onOpen(ConnectionInterface $conn) 
{
    $nickname = $conn->Session->get('nickname');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM