首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
在server的open回调函数里,如何修改外部的变量
我需要将连接带server的client按类型存储到不同的数组里,在外部声明了一个数组,但是在回调函数里只能使用这个数组,下次再调用这个回调函数的时候,数组又为空了 ```php <?php class Server{ private $serv; public $clientArr= array(); public $clientSum = 0; public function __construct(){ $this->serv = new swoole_websocket_server('192.168.1.192', 8180); $this->serv->on('open', function(swoole_websocket_server $serv, $request) use($clientSum, $clientArr){ $clientSum++; echo $clientSum."\n"; $clientArr[count($clientArr)] = $request->fd; foreach($serv->connections as $fd){ echo "$fd-"; } echo "\n"; for($i=0; $i<count($clientArr); $i++){ echo $clientArr[$i].'-'; } echo "\n"; }); $this->serv->on('message', function(swoole_websocket_server $serv, $frame){ echo "Get a msg is: $frame->data\n"; }); $this->serv->on('close', function($ser, $fd){ echo "$fd is close\n"; }); $this->serv->start(); } } $server = new Server(); ```
发布于8年前 · 1 次浏览 · 来自
提问
E
Eamon
我需要将连接带server的client按类型存储到不同的数组里,在外部声明了一个数组,但是在回调函数里只能使用这个数组,下次再调用这个回调函数的时候,数组又为空了 ```php <?php class Server{ private $serv; public $clientArr= array(); public $clientSum = 0; public function __construct(){ $this->serv = new swoole_websocket_server('192.168.1.192', 8180); $this->serv->on('open', function(swoole_websocket_server $serv, $request) use($clientSum, $clientArr){ $clientSum++; echo $clientSum."\n"; $clientArr[count($clientArr)] = $request->fd; foreach($serv->connections as $fd){ echo "$fd-"; } echo "\n"; for($i=0; $i<count($clientArr); $i++){ echo $clientArr[$i].'-'; } echo "\n"; }); $this->serv->on('message', function(swoole_websocket_server $serv, $frame){ echo "Get a msg is: $frame->data\n"; }); $this->serv->on('close', function($ser, $fd){ echo "$fd is close\n"; }); $this->serv->start(); } } $server = new Server(); ```
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2017-04-04
D
David
变量作用域完全是PHP的语法。回调函数可以使用use将对象引入。
赞
0
回复
微信公众号
热门内容
作者其它话题
- HTTP 长连接客户端关闭时服务器爆出错误提示
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。