Ratchet without Symfony session

2019-08-04 20:40发布

问题:

I want to work with ratchet without Symfony session and handle session with php handler between my web application and ratchet. but it doesn't work.

My code for session handling:

Run server : session.php`

ini_set('session.save_handler', 'memcached' );
ini_set('session.save_path', 'localhost:11211' );

use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;

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

    $server = IoServer::factory(
    new WsServer(
    new Chat()
    )
    , 8080
);

    $server->run();

My app: chat.php

public function onOpen(ConnectionInterface $conn) {

        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        session_start();

        echo "New connection! ({$conn->resourceId})\n";
        $conn->send('Hello ' . session_id());

Client side :

ini_set('session.save_handler', 'memcached' );
ini_set('session.save_path', 'localhost:11211' );

session_start();

require __DIR__ . './../server/vendor/autoload.php';

$_SESSION['name'] = $_GET['name'];

if (isset($_SESSION['name'])) {
    var_dump($_SESSION['name']);
} else {
    echo 'Not set!!!';
}

My url for request : localhost/myfile/?name=shahrokh

回答1:

This won't work. From the Ratchet doc:

In order to access your session data in Ratchet, you must also use the same Symfony Session Handler on you website.