[英]Symfony Session Handler for Ratchet
I'm trying to get Ratchet to work with Memcache/Symfony sessions but I can't get it to work. 我正在尝试让Ratchet与Memcache / Symfony会话一起使用,但是我无法使其正常工作。
App runs without any error but I can't seem to get or set session variables. 应用程序运行无任何错误,但我似乎无法获取或设置会话变量。 The socket server and apache wont communicate, even if I specify session handler on the page itself (same on both ends)
即使我在页面本身上指定了会话处理程序,套接字服务器和apache也不会通信(两端相同)
Server: 服务器:
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();
App: 应用程式:
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();
}
}
Can you help me get both ends to communicate? 您能帮我两端沟通吗?
您需要在同一端口上运行网络服务器和网络套接字服务器,否则cookie无法共享(因此您需要在与网络服务器不同的服务器上运行网络套接字)
Turns out what I needed to do was start app on the html root, which doesn't seem secure. 原来我需要做的是在html根目录上启动应用程序,这似乎并不安全。 Further reading required.
需要进一步阅读。 Thank you.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.