ratchetphp / Ratchet

Asynchronous WebSocket server
http://socketo.me
MIT License
6.27k stars 738 forks source link

Session is NULL Memcached #598

Open PenguinArts opened 6 years ago

PenguinArts commented 6 years ago

I ahve just installed Memcached and used tutorial to set up the server.

This is my push-server.php

<?php
use Ratchet\Session\SessionProvider;
use Symfony\Component\HttpFoundation\Session\Storage\Handler;

    require dirname(__DIR__) . '/vendor/autoload.php';

    $loop   = React\EventLoop\Factory::create();
    $pusher = new MyApp\Pusher;

    $memcached = new Memcached;
    $memcached->addServer('localhost', 11211);

    // Listen for the web server to make a ZeroMQ push after an ajax request
    $context = new React\ZMQ\Context($loop);
    $pull = $context->getSocket(ZMQ::SOCKET_PULL);
    $pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
    $pull->on('message', array($pusher, 'onBlogEntry'));

    // Set up our WebSocket server for clients wanting real-time updates
    $webSock = new React\Socket\Server('0.0.0.0:8080', $loop); // Binding to 0.0.0.0 means remotes can connect
    $webServer = new Ratchet\Server\IoServer(
        new Ratchet\Http\HttpServer(
            new Ratchet\Session\SessionProvider(
                new Ratchet\WebSocket\WsServer(
                    new Ratchet\Wamp\WampServer(
                        $pusher
                    )
                ),new Handler\MemcachedSessionHandler($memcached)
            ) 
        ),
        $webSock
    );

    $loop->run();

And onOpen function:

public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
        $this->users[$conn->resourceId] = $conn;

        var_dump($conn->Session->all());

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

    }

Var dump response:

array(0) { } New connection! (62)

Is it something I did not set up right?

EDIT: var_dump for $conn->httpRequest->getHeader('cookie'); return: array(1) { [0]=> string(376) "_ga=GA1.2.1243927159.1515344807; _at_id.eig.eigorem.5517=9cc380a97448ae97.1515344809.1.1515344809.1515344809.0.0.00H; addshoppers.com=2%7C1%3A0%7C10%3A1515344811%7C15%3Aaddshoppers.com%7C44%3AZDcwNmViZGMxMGQ1NDM4OWFiOTllOGM3YzA5MWZmYjI%3D%7Ca78ac69b89c6626730b88e9b11a0488e6f4204c8a98bf3ae260c973e5be7bb3e; timezone=Europe/Helsinki; ci_session=0g3912q6d4q2go4hme61b0otde12po3q" } I need this to send message to a particular user. I want to add database user ID to connection.

PenguinArts commented 6 years ago

I made a few changes and now the session is working but not as I would. Now push-server.php looks like this:

<?php
use Symfony\Component\HttpFoundation\Session\Session;
use Ratchet\Session\SessionProvider;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\Handler;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler;

    require dirname(__DIR__) . '/vendor/autoload.php';

    $loop   = React\EventLoop\Factory::create();
    $pusher = new MyApp\Pusher;

    $memcached = new Memcached();
    $memcached->addServer('0.0.0.0', 11211);
    $memcached->set('site', 'test');

    $storage = new NativeSessionStorage(array(), new MemcachedSessionHandler($memcached));
    $session = new Session($storage);
    $session->start();
    $session->set('name2', 'test');

    // Listen for the web server to make a ZeroMQ push after an ajax request
    $context = new React\ZMQ\Context($loop);
    $pull = $context->getSocket(ZMQ::SOCKET_PULL);
    $pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
    $pull->on('message', array($pusher, 'onBlogEntry'));

    // Set up our WebSocket server for clients wanting real-time updates
    $webSock = new React\Socket\Server('0.0.0.0:8080', $loop); // Binding to 0.0.0.0 means remotes can connect
    $webServer = new Ratchet\Server\IoServer( 
        new Ratchet\Http\HttpServer(
            new Ratchet\Session\SessionProvider(
                new Ratchet\WebSocket\WsServer(
                    new Ratchet\Wamp\WampServer(     
                        $pusher
                    )
                ),new Handler\MemcachedSessionHandler($memcached)
            ) 
        ),
        $webSock
    );

    $loop->run(); 

I use native session and memcached but in the server console when I run php -f push-server.php apper only name2 : test nothing from memcached. This is working only if I set $session->set('name2', 'test'); in push-server.php, when I set it in codeigniter controller it does not work anymore.

I have tested many things, but none of it works. I need some advices. I want to say I use Codeigniter maybe it might be a better decision to move all on Symfony ?

Thanks!

PenguinArts commented 6 years ago

I moved everything on symfony. Now I save session in database. Everything is working great, I am using wss too and is working. The single problem that I have is the following:

[26-Feb-2018 10:02:06 UTC] PHP Warning: Error while sending QUERY packet. PID=22198 in /home/penguiw0/public_html/evadastudio/vendor/symfony/http-foundation/Session/Storage/Handler/PdoSessionHandler.php on line 523

I noticed the error show up when the browser is open and the PC enter in sleep mode, then when I try to change anything on website the above error come out. Is there something I can do?

Thank you!

glodfinch commented 6 years ago

This sounds a lot like an issue I was having, so I figured I'd share my issue here in case someone needs it. I was getting an empty array but PHPSESSID was set correctly as OP describes. Turns out Symfony will only read $_SESSION variables that are inside the _sf2_attributes key. I haven't used Symfony before this, and the codebase I'm using Ratchet with doesn't use it, so for compatibility I had to duplicate the necessary session data inside that array. Unless I missed it this isn't documented on Ratchet's website, I had to read Symfony's source to find it out (as I was lacking the correct search terms).

PenguinArts commented 6 years ago

Thank you. I solved the problem with session after I saved the session in database. Now I try to figure out why in some cases the server stops. How to behave:

I have website and connection open. Then, if the laptop enter in sleep mode -> re-open the laptop and try to make any change on website in error_log appear the following error: [26-Feb-2018 10:02:06 UTC] PHP Warning: Error while sending QUERY packet. PID=22198 in /home/penguiw0/public_html/evadastudio/vendor/symfony/http-foundation/Session/Storage/Handler/PdoSessionHandler.php on line 523

If the laptop stay in sleep mode for instance 1 minute everything is still working. But when it stay in sleep mode more then half an hour I guess the error show up. Any advice?

EDIT: I added var_dump in onError function and this error show up: ["message":protected]=> string(63) "SQLSTATE[HY000]: General error: 2006 MySQL server has gone away"