Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/Gateway/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Connection implements ConnectionInterface

private const QUERY_DATA = ['v' => self::DISCORD_VERSION];

private const HEARTBEAT_ACK_TIMEOUT = 2.5;
private static float $heartbeatAckTimeout = 5;

private ?int $sequence = null;

Expand All @@ -57,6 +57,26 @@ class Connection implements ConnectionInterface

private ShardInterface $shard;

/**
* Change how long to wait for a heartbeat acknowledgement from Discords gateway. This essentially changes how much
* wiggle room your websocket connection allows for.
*
* If you're not sure you need to change this value, you should probably leave it at the default. Lowering the value
* is not recommended, as this will decrease stability.
*
* This function should be called at the start of your application. Before a connection is established. Otherwise
* this change may only take effect after the first connection resume/restart.
*/
public static function setHeartbeatAckTimeout(float $timeout): void
{
self::$heartbeatAckTimeout = $timeout;
}

public static function getHeartbeatAckTimeout(): float
{
return self::$heartbeatAckTimeout;
}

public function __construct(
private LoopInterface $loop,
private string $token,
Expand Down Expand Up @@ -232,7 +252,7 @@ public function sendHeartbeat(): void

private function expectHeartbeatAcknowledgement(): void
{
$this->unacknowledgedHeartbeatTimer = $this->loop->addTimer(self::HEARTBEAT_ACK_TIMEOUT, function () {
$this->unacknowledgedHeartbeatTimer = $this->loop->addTimer(self::$heartbeatAckTimeout, function () {
$this->meta->emit(MetaEvents::UNACKNOWLEDGED_HEARTBEAT, [$this, $this->logger]);
});
}
Expand Down