php – 使用memcache进行棘轮会话数据同步
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 使用memcache进行棘轮会话数据同步,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2281字,纯文字阅读大概需要4分钟。
内容图文
![php – 使用memcache进行棘轮会话数据同步](/upload/InfoBanner/zyjiaocheng/787/c66f888cf85e43cab1a21c73f94135d2.jpg)
我创建了一个棘轮Web套接字服务器,并尝试使用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() {
}
};
Server脚本的工作原理如下所示:
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,则数据已更新.
解决方法:
It seems to be the case that Ratchet reads the session-data once on
connect and after that the session object is independent.
是的,这就是它的工作方式.
https://groups.google.com/d/topic/ratchet-php/1wp1U5c12sU/discussion
内容总结
以上是互联网集市为您收集整理的php – 使用memcache进行棘轮会话数据同步全部内容,希望文章能够帮你解决php – 使用memcache进行棘轮会话数据同步所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。