php7 监听 scoket,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 "内部链接关闭";

}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值