Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Jun 28, 2024
1 parent 34feb59 commit 2f5dc2a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
73 changes: 62 additions & 11 deletions src/PushServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Workbunny\WebmanPushServer;

use Exception;
use Workbunny\WebmanPushServer\Events\AbstractEvent;
use Workbunny\WebmanPushServer\Events\Unsubscribe;
use Workbunny\WebmanPushServer\Traits\ChannelMethods;
Expand All @@ -22,7 +21,6 @@
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Timer;
use Workerman\Worker;

class PushServer
{
Expand Down Expand Up @@ -70,14 +68,14 @@ class PushServer
protected ?int $_heartbeatTimer = null;

/** @var int 心跳 */
protected int $_keepaliveTimeout = 60;
protected int $_keepaliveTimeout;

/** @var AbstractEvent|null 最近一个事件 */
protected ?AbstractEvent $_lastEvent = null;

public function __construct()
{
$this->_keepaliveTimeout = self::getConfig('heartbeat', 60);
$this->_keepaliveTimeout = (int)self::getConfig('heartbeat', 60);
}

/**
Expand Down Expand Up @@ -106,9 +104,9 @@ public function onWorkerStart(): void
// 通道订阅
static::subscribe();
// 心跳设置
if ($this->_keepaliveTimeout > 0 and !$this->_heartbeatTimer) {
$this->_heartbeatTimer = Timer::add(
round($this->_keepaliveTimeout / 2, 2),
if ($this->getKeepaliveTimeout() > 0 and !$this->getHeartbeatTimer()) {
$this->setHeartbeatTimer(Timer::add(
round($this->getKeepaliveTimeout() / 2, 2),
function () {
/**
* @var string $appKey
Expand All @@ -121,6 +119,7 @@ function () {
*/
foreach ($connections as $socketId => $connection) {
$count = static::_getConnectionProperty($connection, 'clientNotSendPingCount');
dump($connection, $count);
if ($count === null or $count > 1) {
$connection->destroy();
static::_unsetConnection($appKey, $socketId);
Expand All @@ -129,17 +128,18 @@ function () {
static::_setConnectionProperty($connection, 'clientNotSendPingCount', $count + 1);
}
}
});
})
);
}
}

/**
* @return void
*/
public function onWorkerStop(): void{
if ($this->_heartbeatTimer){
Timer::del($this->_heartbeatTimer);
$this->_heartbeatTimer = 0;
if ($this->getHeartbeatTimer()){
Timer::del($this->getHeartbeatTimer());
$this->setHeartbeatTimer(null);
}
static::close();
}
Expand Down Expand Up @@ -252,6 +252,57 @@ public function setLastEvent(?AbstractEvent $lastEvent): void
$this->_lastEvent = $lastEvent;
}

/**
* @return int|null
*/
public function getHeartbeatTimer(): ?int
{
return $this->_heartbeatTimer;
}

/**
* @param int|null $heartbeatTimer
* @return void
*/
public function setHeartbeatTimer(?int $heartbeatTimer): void
{
$this->_heartbeatTimer = $heartbeatTimer;
}

/**
* @return int
*/
public function getKeepaliveTimeout(): int
{
return $this->_keepaliveTimeout;
}

/**
* @param int $keepaliveTimeout
* @return void
*/
public function setKeepaliveTimeout(int $keepaliveTimeout): void
{
$this->_keepaliveTimeout = $keepaliveTimeout;
}

/**
* @return \Workerman\Connection\TcpConnection[][]
*/
public static function getConnections(): array
{
return self::$_connections;
}

/**
* @param array $connections
* @return void
*/
public static function setConnections(array $connections): void
{
self::$_connections = $connections;
}

/**
* 向连接发送错误消息
*
Expand Down
2 changes: 0 additions & 2 deletions src/config/plugin/workbunny/webman-push-server/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

return [
'enable' => true,
// 心跳检查, 0为不检查
'heartbeat' => 60,
// 验证app_key
'app_verify' => function (string $appKey): array
{
Expand Down

0 comments on commit 2f5dc2a

Please sign in to comment.