使用 memcache 的棘轮会话数据同步 [英] Ratchet Session Data Synchronisation using memcache

查看:35
本文介绍了使用 memcache 的棘轮会话数据同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Ratchet Web Socket Server 并尝试使用 SESSIONS.

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

在 HTTP 网络服务器(端口 80)上的 php 文件中,我像这样设置会话数据

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

并使用 Javascript 连接到 Ratchet Websocket 服务器

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

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

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

    }
}

它有效.如果我在连接到 websocket 之前设置了会话数据,那么 uname 在我的套接字服务器脚本中是可获取的.

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

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

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.

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

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.

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

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.

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

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

推荐答案

似乎是 Ratchet 读取会话数据一次连接,然后会话对象是独立的.

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/棘轮php/1wp1U5c12sU/讨论

这篇关于使用 memcache 的棘轮会话数据同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆