4

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

在 HTTP-Webserver(端口 80)上的我的 php 文件中,我像这样设置会话数据

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 服务器

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 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 在我的套接字服务器脚本中是可访问的。

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

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

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

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

4

1 回答 1

3

似乎 Ratchet 在连接时读取会话数据一次,之后会话对象是独立的。

是的,这就是它的工作方式。

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

于 2015-02-04T02:44:22.450 回答