首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
协程组件只能在服务器的onConnect、onRequest、onReceive、onMessage 回调函数中使用的问题
我有如下代码: {{{ class Server { public $server; public $redisPool = []; public function run() { $this->server = new Swoole\Http\Server("0.0.0.0", 9501, SWOOLE_BASE); $this->server->set([ 'worker_num' => 1, ]); $this->server->on('Connect', [$this, 'onConnect']); $this->server->on('Request', [$this, 'onRequest']); $this->server->on('Close', [$this, 'onClose']); $this->server->start(); } public function onConnect($serv, $fd, $reactorId) { } public function onClose($serv, $fd, $reactorId) { } public function onRequest($request, $response) { $obj = new M(); $rs = $obj->todo(); var_dump($rs); $response->end('xxxx'); } } $server = new Server(); $server->run(); class M{ function todo() { $swoole_mysql = new Swoole\Coroutine\MySQL(); $swoole_mysql->connect(['host' => '127.0.0.1', 'user' => 'root', 'password' => 'hello123', 'database' => 'putao_admin']); $res = $swoole_mysql->query('select sleep(1)'); return $res; } } }}} 执行后浏览器访问http://127.0.0.1:9501,页面会一直loading 我看到官方文档有说"协程组件只能在服务器的onConnect、onRequest、onReceive、onMessage 回调函数中使用" 我想问的是,难道我封装个有协程组件的类,然后去调用都不行吗? 是必须在这些服务器回调函数中使用吗?
发布于6年前 · 7 次浏览 · 来自
提问
j
jayfir
我有如下代码: {{{ class Server { public $server; public $redisPool = []; public function run() { $this->server = new Swoole\Http\Server("0.0.0.0", 9501, SWOOLE_BASE); $this->server->set([ 'worker_num' => 1, ]); $this->server->on('Connect', [$this, 'onConnect']); $this->server->on('Request', [$this, 'onRequest']); $this->server->on('Close', [$this, 'onClose']); $this->server->start(); } public function onConnect($serv, $fd, $reactorId) { } public function onClose($serv, $fd, $reactorId) { } public function onRequest($request, $response) { $obj = new M(); $rs = $obj->todo(); var_dump($rs); $response->end('xxxx'); } } $server = new Server(); $server->run(); class M{ function todo() { $swoole_mysql = new Swoole\Coroutine\MySQL(); $swoole_mysql->connect(['host' => '127.0.0.1', 'user' => 'root', 'password' => 'hello123', 'database' => 'putao_admin']); $res = $swoole_mysql->query('select sleep(1)'); return $res; } } }}} 执行后浏览器访问http://127.0.0.1:9501,页面会一直loading 我看到官方文档有说"协程组件只能在服务器的onConnect、onRequest、onReceive、onMessage 回调函数中使用" 我想问的是,难道我封装个有协程组件的类,然后去调用都不行吗? 是必须在这些服务器回调函数中使用吗?
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2018-05-24
若
若愚
因为只有这些回调函数中底层创建了协程,所以只能在这些回调函数中使用协程客户端。
赞
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一直是同一个。没用使用到多进程啊。