JDare / ClankBundle

A Symfony2 Bundle for use with Ratchet WebSocket Server
MIT License
131 stars 31 forks source link

Access Session within Topic #9

Closed teagup closed 11 years ago

teagup commented 11 years ago

Hi, I'm trying to access the session after following along with the "Session Sharing" documentation, but I'm running into issues trying to access session variables. Currently I get the error: "Call to a member function get() on a non-object". I'm passing the request service into the Topic constructor and accessing the session through "$request->getSession();", but it's not working. Btw, I'm using mongodb to store the session data. Any hints on how I can access the session from within the Topic?

teagup commented 11 years ago

Ok, so I figured out that I should be using $conn->Session in my Topic, in order to retrieve the user's session, but the problem I'm having now is that the session doesn't have any of the attributes as the web app. Is this an issue with using mongodb as the session handler? Or is it an issue with the URLs or something?

teagup commented 11 years ago

Ok, so looks like it was an issue with using localhost vs 127.0.0.1 in my config.yml and parameters.yml files. You should use localhost for both (at least for local development), once you do cookies are sent along with all websocket requests, allowing session data to be accessed from the db.

alcalyn commented 10 years ago

I got the same problem, in my case I solved it by starting the session both in server, and in every client connections:

<?php

namespace MyApp\CoreBundle\EventListener;

use Symfony\Component\HttpFoundation\Session\Session;
use JDare\ClankBundle\Event\ServerEvent;
use JDare\ClankBundle\Event\ClientEvent;

class ClankEventListener
{
    /**
     * @var Session
     */
    private $session;

    /**
     * @param Session $session
     */
    public function __construct(Session $session)
    {
        $this->session = $session;
    }

    /**
     * Called on clank server launched.
     * 
     * @param \JDare\ClankBundle\Event\ServerEvent $event
     */
    public function onServerLaunched(ServerEvent $event)
    {
        // Start server session (don't know why but works)
        $this->session->start();

        echo 'Session initialized'.PHP_EOL;
    }

    /**
     * Called whenever a client connects,
     * Start connection session
     *
     * @param ClientEvent $event
     */
    public function onClientConnect(ClientEvent $event)
    {
        $conn = $event->getConnection();

        // Start client session
        $conn->Session->start();

        echo $conn->Session->get('user')->getPseudo() . ' connected' . PHP_EOL;

        $event->stopPropagation();
    }
}

And services.yml:

services:
    core.clank_listener:
        class: MyApp\CoreBundle\EventListener\ClankEventListener
        arguments: [@session]
        tags:
            - { name: kernel.event_listener, event: clank.server.launched , method: onServerLaunched }
            - { name: kernel.event_listener, event: clank.client.connected, method: onClientConnect, priority: 5 }