Обработчик сессий Symfony для Ratchet

Я пытаюсь заставить Ratchet работать с сессиями Memcache/Symfony, но я не могу заставить его работать.

Приложение работает без ошибок, но я не могу получить или установить переменные сеанса. Сервер сокетов и apache не будут общаться, даже если я укажу обработчик сеанса на самой странице (то же самое на обоих концах)

Сервер:

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\Http\Router;
use Ratchet\Wamp\WampServer;
use Ratchet\Session\SessionProvider;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\Handler;
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
use Symfony\Component\HttpFoundation\Session\Storage;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Ratchet\App;

ini_set('session.cookie_domain', '.domain.com:80');
ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', 'localhost:11211');

session_start();

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

$storage = new NativeSessionStorage(array());
$session = new Session($storage, new NamespacedAttributeBag());

$request = Request::createFromGlobals();
$request->setSession($session);

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

$session = new SessionProvider(
    new \App\Mire
  , new Handler\MemcacheSessionHandler($memcache)
);

$server = new Ratchet\App('www.domain.com', 8081, '0.0.0.0');
$server->route('/sess', $session);
$server->run();

Приложение:

namespace App;

use Symfony\Component\HttpFoundation\Session\Storage\Handler;
use Ratchet\App;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Session\SessionProvider;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;

class Mire implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = array();
    }

    public function onOpen(\Ratchet\ConnectionInterface $conn) {
        $this->clients[$conn->resourceId] = $conn;
        echo "New connection! ({$conn->resourceId})\n";

        $sessao = new \Symfony\Component\HttpFoundation\Session\Session(new \Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage());
        $sessao->start();

        echo "io:".$conn->Session->get("io");
    }

    public function onMessage(ConnectionInterface $from, $msg) {
    }

    public function onClose(ConnectionInterface $conn) {
        unset($this->clients[$conn->resourceId]);
        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";
        $conn->close();
    }
}

Можете ли вы помочь мне общаться друг с другом?

2 ответа

Вам необходимо запустить ваш веб-сервер и сервер веб-сокетов на одном и том же порту, в противном случае файл cookie не может быть общим (поэтому вам нужно запустить веб-сокет на другом сервере, чем веб-сервер)

Оказалось, что мне нужно было запустить приложение в корне html, которое не выглядит безопасным. Требуется дальнейшее чтение. Спасибо.

Другие вопросы по тегам