使用memcache的棘轮会话数据同步



我创建了一个Ratchet Web套接字服务器,并尝试使用SESSIONS。

在HTTP Web服务器(端口80)上的php文件中,我设置了如下的会话数据

use SymfonyComponentHttpFoundationSessionSession;
use SymfonyComponentHttpFoundationSessionStorageNativeSessionStorage;
use SymfonyComponentHttpFoundationSessionStorageHandlerMemcacheSessionHandler;
$memcache = new Memcache;
$memcache->connect('localhost', 11211);
$storage = new NativeSessionStorage(array(), new MemcacheSessionHandler($memcache));
$session = new Session($storage);
$session->start();
$session->set('uname', $uname);

并通过Javascript 连接到Ratchet Websocket服务器

var RatchetClient = {
    url: "ws://192.168.1.80:7070",
    ws: null,
    init: function() {
        var root = this;
        this.ws = new WebSocket(RatchetClient.url);
        this.ws.onopen = function(e) {
            console.log("Connection established!");
            root.onOpen();
        };
        this.ws.onmessage = function(evt) {
            console.log("Message Received : " + evt.data);
            var obj = JSON.parse(evt.data);
            root.onMessage(obj);
        };
        this.ws.onclose = function(CloseEvent) {
        };
        this.ws.onerror = function() {
        };
    },
    onMessage : function(obj) {    
    },
    onOpen : function() {        
    }
};

服务器脚本的工作方式如下所述:http://socketo.me/docs/sessions

如果客户端发送消息,我获取会话数据

$memcache = new Memcache;
$memcache->connect('localhost', 11211);
$session = new SessionProvider(
    new MyServer()
  , new HandlerMemcacheSessionHandler($memcache)
);

$server = IoServer::factory(
    new HttpServer(
        new WsServer($session)
    )
  , 7070
);
$server->run();

class MyServer implements MessageComponentInterface {
    public function onMessage(ConnectionInterface $conn, $msg) {
        $name = $conn->Session->get("uname");
    }
}

它有效。如果我在连接到websocket之前设置了会话数据,那么uname在我的socket服务器脚本中是可传输的。

每当我通过ajax或从另一个浏览器窗口更改会话数据时,我正在运行的客户端的会话数据都不会同步。

这意味着,如果我更改uname或销毁会话,套接字服务器将无法识别这一点。这种情况似乎是Ratchet在连接时读取会话数据一次,之后会话对象是独立的。

你能证实这种行为吗?或者我做错了什么。我认为使用memcache的目标是能够从不同连接的客户端访问相同的会话数据。

如果在更改会话数据后重新连接到websocket,则数据已更新。

Ratchet似乎读取了一次会话数据connect,然后会话对象是独立的。

是的,就是这样。

https://groups.google.com/d/topic/ratchet-php/1wp1U5c12sU/discussion

最新更新