Php 使用memcache的棘轮会话数据同步
我创建了一个Ratchet Web套接字服务器,并尝试使用会话 在HTTP Web服务器(端口80)上的php文件中,我设置了如下会话数据Php 使用memcache的棘轮会话数据同步,php,session,session-variables,ratchet,Php,Session,Session Variables,Ratchet,我创建了一个Ratchet Web套接字服务器,并尝试使用会话 在HTTP Web服务器(端口80)上的php文件中,我设置了如下会话数据 use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; use Symfony\Component\HttpFoundation\Session\Storag
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() {
}
};
服务器脚本的工作方式如下所述:
如果客户端发送消息,我将获取会话数据
$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在connect上读取会话数据一次,之后会话对象是独立的
你能证实这种行为吗?还是我做错了什么。我认为使用memcache的目标是能够从不同连接的客户端访问相同的会话数据
如果在更改会话数据后重新连接到websocket,则数据已更新
Ratchet一次读取会话数据似乎就是这种情况
连接,然后会话对象是独立的
是的,就是这样