首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
swoole_client 绑定事件问题
我在构造方法里面绑定了事件,但是没有起效,我一样的语法在swoole_server里面是没有问题的! <?php // +---------------------------------------------------------------------- // | date: 2015-06-28 // +---------------------------------------------------------------------- // | SwooleController.php: swoole客户端控制器 // +---------------------------------------------------------------------- // | Author: yangyifan <yangyifanphp@gmail.com> // +---------------------------------------------------------------------- class SwooleController extends Controller { private $swoole_client;//swoole_clent客户端 /** * 构造方法 * * @auther yangyifan <yangyifanphp@gmail.com> */ public function __construct(){ $this->swoole_client = new \swoole_client(SWOOLE_SOCK_TCP); //设置客户端参数 $this->set(); //绑定事件 $this->swoole_client->on('connect', [$this, 'onConnect']); $this->swoole_client->on('receive', [$this, 'onReceive']); $this->swoole_client->on('close', [$this, 'onClose']); //链接swoole_server $this->swoole_client->connect(config('swoole.swoole_host'), config('swoole.swoole_port'), config('swoole.swoole_timeout')); //判断是否连接 if($this->swoole_client->isConnected() == false){ echo ("connect failed. Error: {$this->swoole_client->errCode}".config('swoole.package_eof')); } } /** * 设置客户端参数 * * @auther yangyifan <yangyifanphp@gmail.com> */ private function set(){ $this->swoole_client->set([ 'open_length_check' => config('swoole.open_eof_check'), 'package_eof' => config('swoole.package_eof'), ]); } /** * 发送请求 * * @auther yangyifan <yangyifanphp@gmail.com> */ public function getIndex(){ $this->swoole_client->send("hello world212121".config('swoole.package_eof')); } /** * 客户端连接 * * @param \swoole_client $client * @auther yangyifan <yangyifanphp@gmail.com> */ public function onConnect($client){ echo 'Clent connect'.config('swoole.package_eof'); $client->send("hello world212121".config('swoole.package_eof')); } /** * 接受数据 * * @param \swoole_client $client * @param string $data * @auther yangyifan <yangyifanphp@gmail.com> */ public function onReceive($client, $data = ''){ echo $data; } /** * 客户端关闭 * * @param \swoole_client $client * @auther yangyifan <yangyifanphp@gmail.com> */ public function onClose($client){ echo 'Client close'; } }
发布于8年前 · 0 次浏览 · 来自
提问
十
十年不散
我在构造方法里面绑定了事件,但是没有起效,我一样的语法在swoole_server里面是没有问题的! <?php // +---------------------------------------------------------------------- // | date: 2015-06-28 // +---------------------------------------------------------------------- // | SwooleController.php: swoole客户端控制器 // +---------------------------------------------------------------------- // | Author: yangyifan <yangyifanphp@gmail.com> // +---------------------------------------------------------------------- class SwooleController extends Controller { private $swoole_client;//swoole_clent客户端 /** * 构造方法 * * @auther yangyifan <yangyifanphp@gmail.com> */ public function __construct(){ $this->swoole_client = new \swoole_client(SWOOLE_SOCK_TCP); //设置客户端参数 $this->set(); //绑定事件 $this->swoole_client->on('connect', [$this, 'onConnect']); $this->swoole_client->on('receive', [$this, 'onReceive']); $this->swoole_client->on('close', [$this, 'onClose']); //链接swoole_server $this->swoole_client->connect(config('swoole.swoole_host'), config('swoole.swoole_port'), config('swoole.swoole_timeout')); //判断是否连接 if($this->swoole_client->isConnected() == false){ echo ("connect failed. Error: {$this->swoole_client->errCode}".config('swoole.package_eof')); } } /** * 设置客户端参数 * * @auther yangyifan <yangyifanphp@gmail.com> */ private function set(){ $this->swoole_client->set([ 'open_length_check' => config('swoole.open_eof_check'), 'package_eof' => config('swoole.package_eof'), ]); } /** * 发送请求 * * @auther yangyifan <yangyifanphp@gmail.com> */ public function getIndex(){ $this->swoole_client->send("hello world212121".config('swoole.package_eof')); } /** * 客户端连接 * * @param \swoole_client $client * @auther yangyifan <yangyifanphp@gmail.com> */ public function onConnect($client){ echo 'Clent connect'.config('swoole.package_eof'); $client->send("hello world212121".config('swoole.package_eof')); } /** * 接受数据 * * @param \swoole_client $client * @param string $data * @auther yangyifan <yangyifanphp@gmail.com> */ public function onReceive($client, $data = ''){ echo $data; } /** * 客户端关闭 * * @param \swoole_client $client * @auther yangyifan <yangyifanphp@gmail.com> */ public function onClose($client){ echo 'Client close'; } }
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2016-10-29
q
qzuser
[@rango ](/u/1)虽然没有回答我,但是 我看手册貌似,异步模式仅可用于cli模式 对么?
赞
0
回复
微信公众号
热门内容
暂无回复的问答
- 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一直是同一个。没用使用到多进程啊。