首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
swoole主服务器是websocket服务器,设置内部监听的回调不生效
### 问题描述 swoole主服务器是websocket服务器,设置内部监听的回调不生效 ### Swoole4.4.2版本,PHP7.2.4版本,操作系统ubuntu18.04 ### 相关代码 ```php $this->innerServer = $this->server->addListener($innserConf['host'],$innserConf['port'],SWOOLE_SOCK_TCP); if(empty($innserConf['set'])){ exit; } $this->innerServer->set($innserConf['set']); $this->innerServer->on('connect', [$this, 'onInnerConnect']); $this->innerServer->on('receive', [$this, 'onInnerReceive']); $this->innerServer->on('close', [$this, 'onInnerClose']); $this->innerServer->on('open', [$this, 'onInnerOpen']); 内部监听的配置信息: 'innser_connect' => [ 'host' => '127.0.0.1', 'port' => 9511, 'set' => [ //最大请求次数 0:无上限 'max_request' => 0, //心跳频次 'heartbeat_check_interval' => 60, //心跳空闲时间 'heartbeat_idle_time' => 120, ] ] 内部监听的回调函数: /** * business server连接 * @param \Swoole\Server $server * @param int $fd */ public function onInnerConnect(\Swoole\Server $server, int $fd) { $this->logPrint(LOG_NOTICE, 'inner client connected'); $this->innerClientFd->set($this->server->host.":".$this->server->port."-".$this->server->workerId,$fd); } public function onInnerReceive(\Swoole\Server $server, int $fd,int $from_id,$data) { var_dump($data); } public function onInnerClose(\Swoole\Server $server, $fd) { echo "{$fd}Client: Close.\n"; } 客户端: public function init() { $this->client = new \Swoole\Client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $this->client->set([ 'open_length_check' => true, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4, 'package_max_length' => 81920, ]); $this->client->host = $this->servHost; $this->client->port = $this->servPort; $this->client->on('connect',[$this,'onConnect']); $this->client->on('receive',[$this,'onReceive']); $this->client->on('close',[$this,'onClose']); $this->client->on('error',[$this,'onError']); $this->client->connect($this->servHost,$this->servPort); } public function onError(\Swoole\Client $client) { echo "内部出错"; } public function onConnect(\Swoole\Client $client) { echo "内部链接成功:{$this->servHost}:{$this->servPort}"; } public function onReceive(\Swoole\Client $client) { } public function onClose(\Swoole\Client $client) { echo "内部链接关闭"; } ```
发布于5年前 · 4 次浏览 · 来自
提问
marryML
### 问题描述 swoole主服务器是websocket服务器,设置内部监听的回调不生效 ### Swoole4.4.2版本,PHP7.2.4版本,操作系统ubuntu18.04 ### 相关代码 ```php $this->innerServer = $this->server->addListener($innserConf['host'],$innserConf['port'],SWOOLE_SOCK_TCP); if(empty($innserConf['set'])){ exit; } $this->innerServer->set($innserConf['set']); $this->innerServer->on('connect', [$this, 'onInnerConnect']); $this->innerServer->on('receive', [$this, 'onInnerReceive']); $this->innerServer->on('close', [$this, 'onInnerClose']); $this->innerServer->on('open', [$this, 'onInnerOpen']); 内部监听的配置信息: 'innser_connect' => [ 'host' => '127.0.0.1', 'port' => 9511, 'set' => [ //最大请求次数 0:无上限 'max_request' => 0, //心跳频次 'heartbeat_check_interval' => 60, //心跳空闲时间 'heartbeat_idle_time' => 120, ] ] 内部监听的回调函数: /** * business server连接 * @param \Swoole\Server $server * @param int $fd */ public function onInnerConnect(\Swoole\Server $server, int $fd) { $this->logPrint(LOG_NOTICE, 'inner client connected'); $this->innerClientFd->set($this->server->host.":".$this->server->port."-".$this->server->workerId,$fd); } public function onInnerReceive(\Swoole\Server $server, int $fd,int $from_id,$data) { var_dump($data); } public function onInnerClose(\Swoole\Server $server, $fd) { echo "{$fd}Client: Close.\n"; } 客户端: public function init() { $this->client = new \Swoole\Client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $this->client->set([ 'open_length_check' => true, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4, 'package_max_length' => 81920, ]); $this->client->host = $this->servHost; $this->client->port = $this->servPort; $this->client->on('connect',[$this,'onConnect']); $this->client->on('receive',[$this,'onReceive']); $this->client->on('close',[$this,'onClose']); $this->client->on('error',[$this,'onError']); $this->client->connect($this->servHost,$this->servPort); } public function onError(\Swoole\Client $client) { echo "内部出错"; } public function onConnect(\Swoole\Client $client) { echo "内部链接成功:{$this->servHost}:{$this->servPort}"; } public function onReceive(\Swoole\Client $client) { } public function onClose(\Swoole\Client $client) { echo "内部链接关闭"; } ```
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2020-11-07
鲁飞
没明白你想干什么? 客户端发的消息服务端收不到?
赞
0
回复
2020-11-09
marryML
回复
鲁飞
回调函数不生效,接收不到数据
赞
0
回复
2020-11-09
鲁飞
回复
marryML
客户端服务端的回调函数? 服务端我测试可以。 客户端的你设置了包协议,发送的时候需要封包。
赞
0
回复
2021-02-04
marryML
回复
鲁飞
谢谢大佬,那次重启结果自己好了,最近刚看到所以才回复的。
赞
0
回复