【问题标题】:Symfony Session Handler for Ratchet用于 Ratchet 的 Symfony 会话处理程序
【发布时间】:2015-08-17 22:11:25
【问题描述】:

我正在尝试让 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();
    }
}

你能帮我搞定两端的沟通吗?

【问题讨论】:

    标签: php symfony session ratchet


    【解决方案1】:

    你需要在同一个端口上运行你的webserver和websocket server,否则cookie不能被共享(所以你需要在与webserver不同的服务器上运行websocket)

    【讨论】:

    • 谢谢。我试过设置代理,但仍然无法获得会话。它不应该与“在同一端口上的不同服务器上托管应用程序”具有相同的效果吗?结果,即使我打印会话 id,我也会得到不同的值。
    • 查看有关 session socketo.me/docs/sessions 的文档您不应该在 onOpen 中启动新会话,棘轮会话提供程序正在将会话附加到连接对象
    • 这对我没有帮助。我尝试不在服务器上启动和启动,但都一样。我阅读了文档...从未见过如此糟糕的文档。
    【解决方案2】:

    原来我需要做的是在 html 根目录上启动应用程序,这似乎并不安全。需要进一步阅读。谢谢。

    【讨论】:

      猜你喜欢
      • 2013-07-22
      • 2017-03-01
      • 1970-01-01
      • 2012-10-29
      • 2014-11-26
      • 1970-01-01
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多