首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
swoole 服务器端使用 on回调函数 receive对象变量不共享问题
{{{ public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { echo $this->id++ ."_id_\n"; $data = json_decode($data, true); if($data['behavior'] == 'connect' && $data['group'] == 'proxy'){ echo "proxy client connect success. \n"; $this->proxyClient = $fd; echo $fd."\n"; echo $this->proxyClient."\n"; } if($data['group'] == 'common' && $data['behavior'] == 'req'){ echo "recv a req:". $data['data']."\n"; if(isset($this->proxyClient) && $this->proxyClient){ $this->serv->send($this->proxyClient, rand(1, 1000), "url", $fd); }else{ exit('proxy client no work.'); } } }}} 第一个if $this->proxyClient 保存了 fd, 第二个 if 执行时候 提示 $this->proxyClient没有值 第一次receive 跟第二次receive 是两个client连接的 为什么第二次proxyClient是空值?
发布于9年前 · 3 次浏览 · 来自
提问
W
Where
{{{ public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { echo $this->id++ ."_id_\n"; $data = json_decode($data, true); if($data['behavior'] == 'connect' && $data['group'] == 'proxy'){ echo "proxy client connect success. \n"; $this->proxyClient = $fd; echo $fd."\n"; echo $this->proxyClient."\n"; } if($data['group'] == 'common' && $data['behavior'] == 'req'){ echo "recv a req:". $data['data']."\n"; if(isset($this->proxyClient) && $this->proxyClient){ $this->serv->send($this->proxyClient, rand(1, 1000), "url", $fd); }else{ exit('proxy client no work.'); } } }}} 第一个if $this->proxyClient 保存了 fd, 第二个 if 执行时候 提示 $this->proxyClient没有值 第一次receive 跟第二次receive 是两个client连接的 为什么第二次proxyClient是空值?
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2017-05-19
失
失落的小鲤鱼_
可以使用swoole_table来存链接的fd,两个receive 在不同的进程里面,所以取不到。
赞
0
回复
2017-05-19
a
abba2013a
关于phper常用的全局变量(global)为什么在onRequest函数中不能使用。 因为swoole是多线程编程,global是不能在多个进程间共享的。例 1. global $i = 0; 2. 3. function onRequest() { 4. 5. echo $i++; 6. 7. } 如果在swoole中写一个上面的程序,并不会每次访问输出一个递增的数字。如果要实现预期的效果,需要使用swoole_table的相关函数 谢谢!我去试试
赞
0
回复