Centrifugo-Client
view release on metacpan or search on metacpan
lib/Centrifugo/Client.pm view on Meta::CPAN
# This function is called when client is connected to Centrifugo
sub _on_connect {
my ($this, $body) = @_;
$this->_debug( "Centrifugo::Client : Connected to Centrifugo : ".encode_json $body );
# on Connect, the client_id must be read (if available)
if ($body && ref($body) eq 'HASH' && $body->{client}) {
$this->{CLIENT_ID} = $body->{client};
$this->_debug( "Centrifugo::Client : CLIENT_ID=".$this->{CLIENT_ID} );
}
$this->_init_keep_alive_timer() if $this->{MAX_ALIVE};
$this->_reset_reconnect_sequence();
$this->_resubscribe() if $this->{RESUBSCRIBE};
}
# This function is called when client receives a message
sub _on_message {
my ($this, $body) = @_;
my $uid = $body->{uid};
my $channel = $body->{channel};
$this->_debug( "Centrifugo::Client : Message from $channel : ".encode_json $body->{data} );
lib/Centrifugo/Client.pm view on Meta::CPAN
}
# Reconnects to the server after a loss of connection
# When client disconnected from server it will automatically try to reconnect using
# fibonacci sequence to get interval between reconnect attempts which value grows exponentially. (why not ?)
sub _reconnect {
my ($this) = @_;
my $retry_after = $this->{_next_retry} > $this->{MAX_RETRY} ? $this->{MAX_RETRY} : $this->{_next_retry};
$retry_after = int($retry_after) if $retry_after > 3;
$this->_debug( "Centrifugo::Client : will reconnect after $retry_after s." );
$this->{reconnect_handler} = AnyEvent->timer(
after => $retry_after,
cb => sub {
$this->{_next_retry} += $this->{_last_retry};
$this->{_last_retry} = $retry_after;
$this->_connect();
}
);
}
# Creates the timer to send periodic ping
sub _init_keep_alive_timer {
my ($this) = @_;
$this->{_alive_handler} = AnyEvent->timer(
after => $this->{REFRESH},
interval => $this->{REFRESH},
cb => sub {
my $late = time() - $this->{_last_alive_message};
if ($late > $this->{MAX_ALIVE}) {
$this->_debug( "Sending ping (${late}s without message)" );
$this->ping();
}
}
);
( run in 0.623 second using v1.01-cache-2.11-cpan-49f99fa48dc )