首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
swoole 计时器 访问变量问题
我的问题是这样的 创建了一个类 定义类变量$people,然后创建SWOOLE 和 计时器 这个计时器需要对 $people 数组中的数据遍历,但游戏运行中,$people数组已经改变,当定时器执行时,$people这个数组中的数据不会变 还是空的,这是为什么,怎么解决这个问题,求大神帮忙解决下。代码如下: <?php class Demo_Server { public $people; //玩家数据 /*初始化函数*/ function __construct() { // 创建 Swoole 对象 $this->serv = new swoole_websocket_server("0.0.0.0", 9999); // 设置默认设置 $this->serv->set(array( 'daemonize'=> false, // 守护进程化 设置为 true 则后台运行 )); // 注册回调事件 $this->serv->on('Start', array($this, 'onStart')); // 服务器启动 $this->serv->on('WorkerStart', array($this, 'onWorkerStart'));// 工作进程启动 $this->serv->on('open', array($this, 'onOpen')); // 客户端有连接 $this->serv->on('message', array($this, 'onMessage')); // 收到客户端消息 $this->serv->on('Close', array($this, 'onClose')); // 客户端断开连接 //Swoole 同步MYSQL定时器 Swoole\Timer::tick(10000, function(){//10秒触发 $this->game_file(); }); // 启动服务 $this->serv->start(); } //游戏存档 public function game_file(){ print_r($this->people); } }
发布于4年前 · 17 次浏览 · 来自
提问
赵董
我的问题是这样的 创建了一个类 定义类变量$people,然后创建SWOOLE 和 计时器 这个计时器需要对 $people 数组中的数据遍历,但游戏运行中,$people数组已经改变,当定时器执行时,$people这个数组中的数据不会变 还是空的,这是为什么,怎么解决这个问题,求大神帮忙解决下。代码如下: <?php class Demo_Server { public $people; //玩家数据 /*初始化函数*/ function __construct() { // 创建 Swoole 对象 $this->serv = new swoole_websocket_server("0.0.0.0", 9999); // 设置默认设置 $this->serv->set(array( 'daemonize'=> false, // 守护进程化 设置为 true 则后台运行 )); // 注册回调事件 $this->serv->on('Start', array($this, 'onStart')); // 服务器启动 $this->serv->on('WorkerStart', array($this, 'onWorkerStart'));// 工作进程启动 $this->serv->on('open', array($this, 'onOpen')); // 客户端有连接 $this->serv->on('message', array($this, 'onMessage')); // 收到客户端消息 $this->serv->on('Close', array($this, 'onClose')); // 客户端断开连接 //Swoole 同步MYSQL定时器 Swoole\Timer::tick(10000, function(){//10秒触发 $this->game_file(); }); // 启动服务 $this->serv->start(); } //游戏存档 public function game_file(){ print_r($this->people); } }
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2022-02-07
lanmao
你试试这样 ```php <?php class Demo_Server { public $people; //玩家数据 /*初始化函数*/ function __construct() { // 创建 Swoole 对象 $this->serv = new swoole_websocket_server("0.0.0.0", 9999); // 设置默认设置 $this->serv->set(array( 'daemonize'=> false, // 守护进程化 设置为 true 则后台运行 )); // 注册回调事件 $this->serv->on('Start', array($this, 'onStart')); // 服务器启动 $this->serv->on('WorkerStart', array($this, 'onWorkerStart'));// 工作进程启动 $this->serv->on('open', array($this, 'onOpen')); // 客户端有连接 $this->serv->on('message', array($this, 'onMessage')); // 收到客户端消息 $this->serv->on('Close', array($this, 'onClose')); // 客户端断开连接 $people = $this->people; //Swoole 同步MYSQL定时器 Swoole\Timer::tick(10000, function() use ($people){//10秒触发 $this->game_file($people); }); // 启动服务 $this->serv->start(); } //游戏存档 public function game_file($people){ print_r($people); } } ```
赞
0
回复
2022-02-07
sff
工作进程跟你的定时任务不是在同一个进程里面,数据是隔离的
赞
0
回复