dongxuying7583 2015-08-17 22:11
浏览 60

棘轮的Symfony会话处理程序

I'm trying to get Ratchet to work with Memcache/Symfony sessions but I can't get it to work.

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)

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})
";

        $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
";
    }

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

Can you help me get both ends to communicate?

  • 写回答

2条回答 默认 最新

  • doudong7256 2015-08-18 11:23
    关注

    You need to run your webserver and websocket server on the same port, otherwise the cookie cannot be shared (so you need to run the websocket on a different server than the webserver)

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用