Ratchet Session Data Synchronisation using memcache

扶醉桌前 提交于 2019-12-10 17:40:12

问题


I created a Ratchet Web Socket Server and tried to use SESSIONS.

In my php file on the HTTP-Webserver (Port 80) I set the session-data like this

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler;

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

$storage = new NativeSessionStorage(array(), new MemcacheSessionHandler($memcache));
$session = new Session($storage);
$session->start();

$session->set('uname', $uname);

and connected to the Ratchet Websocket Server with Javascript

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() {        
    }
};

The Server script works like descripted here: http://socketo.me/docs/sessions

If the client sends a message I fetch the session-data

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

$session = new SessionProvider(
    new MyServer()
  , new Handler\MemcacheSessionHandler($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");

    }
}

It works. If i set the session-data before connecting to the websocket then the uname is fechable inside my socket server script.

Whenever I change the session-data via ajax or from another browser window then the session-data of my running client will not been syncronized.

That means if i change the uname or destroy the session the socket server doesn't recognize this. It seems to be the case that Ratchet reads the session-data once on connect and after that the session object is independent.

Can you confirm that behaviour? Or am I doing something wrong. I thought the goal of using memcache is to be able to access the same session-data from different connected clients.

If I do a reconnect to the websocket after changing the session-data then the data has been updated.


回答1:


It seems to be the case that Ratchet reads the session-data once on connect and after that the session object is independent.

Yes, that's the way it works.

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



来源:https://stackoverflow.com/questions/25821199/ratchet-session-data-synchronisation-using-memcache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!