首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
UNIX SOCK 下使用addlistener添加HTTP服务
### 问题描述 创建一个 SWOOLE_SOCK_UNIX_STREAM 服务后希望通过addlistener方法添加一个HTTP服务,但是在文档没找到这样的示例,只有HTTP服务添加其他服务的示例。 ### Swoole版本,PHP版本,以及操作系统版本信息 PHP版本 ```bash root@DESKTOP-E39BL3D:/www/wwwroot/ns# php -v PHP 7.4.13 (cli) (built: Dec 29 2020 17:12:50) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies ``` Swoole版本 ```bash root@DESKTOP-E39BL3D:/www/wwwroot/ns# php --ri swoole swoole Swoole => enabled Author => Swoole Team <team@swoole.com> Version => 4.5.4 Built => Oct 10 2020 20:01:44 coroutine => enabled epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled pcre => enabled zlib => 1.2.11 mutex_timedlock => enabled pthread_barrier => enabled futex => enabled async_redis => enabled Directive => Local Value => Master Value swoole.enable_coroutine => On => On swoole.enable_library => On => On swoole.enable_preemptive_scheduler => Off => Off swoole.display_errors => On => On swoole.use_shortname => Off => Off swoole.unixsock_buffer_size => 8388608 => 8388608 ``` 操作系统信息 ```bash WSL2 - Ubuntu 20.04.1 LTS ``` ```bash 版本 Windows 10 专业版 版本号 21H1 安装日期 2020/6/9 操作系统内部版本 19043.1110 体验 Windows Feature Experience Pack 120.2212.3530.0 ``` ### 相关代码 ```php $host = Env::get('HTTP.HOST','0.0.0.0'); $port = Env::get('HTTP.PORT','8008'); $server = new \Swoole\Server($this->nspmFile, 0, SWOOLE_PROCESS, SWOOLE_SOCK_UNIX_STREAM); $server->set(['worker_num' => 1]); /** * @var bool|\Swoole\Server\Port $http */ $http = $server->addlistener($host,$port,SWOOLE_SOCK_TCP); $http->set([ 'open_http_protocol' => true, // 设置这个端口打开HTTP协议功能 ]); $http->on('request', function (Request $request, Response $response) { $webService = new WebService(new \helper\WebServer\Request($request),new \helper\WebServer\Response($response)); $webService->onRequest(); }); $server->on('start', function (\Swoole\Server $server) { Di::getContainer()->get(OutputInterface::class)->info("onStart"); Di::getContainer()->get(OutputInterface::class)->info("服务器主进程的PID :{$server->master_pid}"); Di::getContainer()->get(OutputInterface::class)->info("服务器管理进程的PID:{$server->manager_pid}"); }); $server->on('shutdown', function (\Swoole\Server $server) { Di::getContainer()->get(OutputInterface::class)->info("onShutdown 服务卸载"); }); $server->on('workerStart', function (\Swoole\Server $server, int $workerId) { Di::getContainer()->get(OutputInterface::class)->info("onWorkerStart , workerId: {$workerId}"); }); $server->on('workerStop', function (\Swoole\Server $server, int $workerId) { Di::getContainer()->get(OutputInterface::class)->info("onWorkerStop , workerId: {$workerId}"); }); $server->on('receive', function (\Swoole\Server $server, $fd, $reactor_id, $data) { }); $server->on('close', function (\Swoole\Server $server, $fd) { Di::getContainer()->get(OutputInterface::class)->info("onClose 服务关闭"); }); $server->start(); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 期待结果:通过浏览器可以打开http服务返回的界面 结果: ```bash Error Swoole\Server::start(): use Swoole\Server class and open http related protocols may lead to some errors (in consistent class type) At File {ROOT}/module/Internet/Service.php line 99 Exception class is ErrorException Code View: 96| Di::getContainer()->get(OutputInterface::class)->info("onClose 服务关闭"); 97| }); 98| > 99| $server->start(); 100| 101| } 102| Code Trace: #0 [internal function]: Inhere\Console\AbstractApplication->handleError() #1 {ROOT}/module/Internet/Service.php(99): Swoole\Server->start() #2 {ROOT}/eventListener/AppStartEventListener.php(20): module\Internet\Service->start() #3 [internal function]: eventListener\AppStartEventListener->execute() #4 {ROOT}/helper/Event/Event.php(43): call_user_func() #5 {ROOT}/helper/Event/Event.php(22): helper\Event\Event::run() #6 {ROOT}/commands/AppCommand.php(40): helper\Event\Event::process() #7 {ROOT}/vendor/inhere/console/src/AbstractHandler.php(262): commands\AppCommand->execute() #8 {ROOT}/vendor/inhere/console/src/Application.php(349): Inhere\Console\AbstractHandler->run() #9 {ROOT}/vendor/inhere/console/src/Application.php(308): Inhere\Console\Application->runCommand() #10 {ROOT}/vendor/inhere/console/src/AbstractApplication.php(242): Inhere\Console\Application->dispatch() #11 {ROOT}/bootstrap/app.php(111): Inhere\Console\AbstractApplication->run() #12 {ROOT}/bin/ns.php(6): bootstrap\app->start() #13 {main} ```
发布于3年前 · 13 次浏览 · 来自
提问
狂奔的菜瓜
### 问题描述 创建一个 SWOOLE_SOCK_UNIX_STREAM 服务后希望通过addlistener方法添加一个HTTP服务,但是在文档没找到这样的示例,只有HTTP服务添加其他服务的示例。 ### Swoole版本,PHP版本,以及操作系统版本信息 PHP版本 ```bash root@DESKTOP-E39BL3D:/www/wwwroot/ns# php -v PHP 7.4.13 (cli) (built: Dec 29 2020 17:12:50) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies ``` Swoole版本 ```bash root@DESKTOP-E39BL3D:/www/wwwroot/ns# php --ri swoole swoole Swoole => enabled Author => Swoole Team <team@swoole.com> Version => 4.5.4 Built => Oct 10 2020 20:01:44 coroutine => enabled epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled pcre => enabled zlib => 1.2.11 mutex_timedlock => enabled pthread_barrier => enabled futex => enabled async_redis => enabled Directive => Local Value => Master Value swoole.enable_coroutine => On => On swoole.enable_library => On => On swoole.enable_preemptive_scheduler => Off => Off swoole.display_errors => On => On swoole.use_shortname => Off => Off swoole.unixsock_buffer_size => 8388608 => 8388608 ``` 操作系统信息 ```bash WSL2 - Ubuntu 20.04.1 LTS ``` ```bash 版本 Windows 10 专业版 版本号 21H1 安装日期 2020/6/9 操作系统内部版本 19043.1110 体验 Windows Feature Experience Pack 120.2212.3530.0 ``` ### 相关代码 ```php $host = Env::get('HTTP.HOST','0.0.0.0'); $port = Env::get('HTTP.PORT','8008'); $server = new \Swoole\Server($this->nspmFile, 0, SWOOLE_PROCESS, SWOOLE_SOCK_UNIX_STREAM); $server->set(['worker_num' => 1]); /** * @var bool|\Swoole\Server\Port $http */ $http = $server->addlistener($host,$port,SWOOLE_SOCK_TCP); $http->set([ 'open_http_protocol' => true, // 设置这个端口打开HTTP协议功能 ]); $http->on('request', function (Request $request, Response $response) { $webService = new WebService(new \helper\WebServer\Request($request),new \helper\WebServer\Response($response)); $webService->onRequest(); }); $server->on('start', function (\Swoole\Server $server) { Di::getContainer()->get(OutputInterface::class)->info("onStart"); Di::getContainer()->get(OutputInterface::class)->info("服务器主进程的PID :{$server->master_pid}"); Di::getContainer()->get(OutputInterface::class)->info("服务器管理进程的PID:{$server->manager_pid}"); }); $server->on('shutdown', function (\Swoole\Server $server) { Di::getContainer()->get(OutputInterface::class)->info("onShutdown 服务卸载"); }); $server->on('workerStart', function (\Swoole\Server $server, int $workerId) { Di::getContainer()->get(OutputInterface::class)->info("onWorkerStart , workerId: {$workerId}"); }); $server->on('workerStop', function (\Swoole\Server $server, int $workerId) { Di::getContainer()->get(OutputInterface::class)->info("onWorkerStop , workerId: {$workerId}"); }); $server->on('receive', function (\Swoole\Server $server, $fd, $reactor_id, $data) { }); $server->on('close', function (\Swoole\Server $server, $fd) { Di::getContainer()->get(OutputInterface::class)->info("onClose 服务关闭"); }); $server->start(); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 期待结果:通过浏览器可以打开http服务返回的界面 结果: ```bash Error Swoole\Server::start(): use Swoole\Server class and open http related protocols may lead to some errors (in consistent class type) At File {ROOT}/module/Internet/Service.php line 99 Exception class is ErrorException Code View: 96| Di::getContainer()->get(OutputInterface::class)->info("onClose 服务关闭"); 97| }); 98| > 99| $server->start(); 100| 101| } 102| Code Trace: #0 [internal function]: Inhere\Console\AbstractApplication->handleError() #1 {ROOT}/module/Internet/Service.php(99): Swoole\Server->start() #2 {ROOT}/eventListener/AppStartEventListener.php(20): module\Internet\Service->start() #3 [internal function]: eventListener\AppStartEventListener->execute() #4 {ROOT}/helper/Event/Event.php(43): call_user_func() #5 {ROOT}/helper/Event/Event.php(22): helper\Event\Event::run() #6 {ROOT}/commands/AppCommand.php(40): helper\Event\Event::process() #7 {ROOT}/vendor/inhere/console/src/AbstractHandler.php(262): commands\AppCommand->execute() #8 {ROOT}/vendor/inhere/console/src/Application.php(349): Inhere\Console\AbstractHandler->run() #9 {ROOT}/vendor/inhere/console/src/Application.php(308): Inhere\Console\Application->runCommand() #10 {ROOT}/vendor/inhere/console/src/AbstractApplication.php(242): Inhere\Console\Application->dispatch() #11 {ROOT}/bootstrap/app.php(111): Inhere\Console\AbstractApplication->run() #12 {ROOT}/bin/ns.php(6): bootstrap\app->start() #13 {main} ```
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2021-07-23
鲁飞
你这个代码如果只跑单文件的话是可以运行的,但是框架中将`Warning`转成了`Error`,导致运行失败。 ``` use Swoole\Server class and open http related protocols may lead to some errors (inconsistent class type) ``` 将这个翻译一下你就知道为什么报错了。 所以文档也是提供的是主服务为`HTTP`或`WebSokcet`,子服务为`TCP`
赞
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一直是同一个。没用使用到多进程啊。