使用Ratchetphp Memcache无法设置或获取会话变量

时间:2019-11-13 15:07:14

标签: php symfony ratchet

我关注了文档here 但是,我似乎无法设置或获取会话数据。它给出的错误是: 调用未定义的方法Ratchet \ Session \ SessionProvider :: start() 基本上我不确定如何将某些内容保存到会话中。我觉得我一路上误解了一些基本知识,陷入了错误尝试的循环中。...

    $memcache = new Memcache;
        $memcache->connect('localhost', 11211);

        $session = new SessionProvider(
            $chat,
            new Handler\MemcacheSessionHandler($memcache)
        );
        $session->start();
        $session->set('username', 'someguy');
        $server = IoServer::factory(
            new HttpServer(
                new WsServer(
                    $session
                )
            ),
            '8080',
            'localhost'
        );

        $server->run();

我想做的是设置一个可以在onMessage()中访问的会话变量:

class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
    $session = $conn->Session;
    $session->start();
    var_dump($conn->Session->get('name'));

        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {    
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
        $msg = $this->translate($msg);

        foreach ($this->clients as $client) {
                $client->send($conn->Session->get('name'). $msg);
        }
    }

0 个答案:

没有答案