1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nucleus - XMPP Library for PHP |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2016, Some rights reserved. |
6
|
|
|
* |
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Contact with author: |
10
|
|
|
* Xmpp: [email protected] |
11
|
|
|
* E-mail: [email protected] |
12
|
|
|
* |
13
|
|
|
* From Kadet with love. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Kadet\Xmpp\Module; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Kadet\Xmpp\Stanza\Stanza; |
20
|
|
|
use Kadet\Xmpp\Xml\XmlElement; |
21
|
|
|
use Kadet\Xmpp\XmppClient; |
22
|
|
|
|
23
|
|
|
class PingKeepAlive extends ClientModule |
24
|
|
|
{ |
25
|
|
|
private $_interval = 15; |
26
|
|
|
private $_timer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* PingKeepAlive constructor. |
30
|
|
|
* |
31
|
|
|
* @param float $interval Keep alive interval in seconds |
32
|
|
|
*/ |
33
|
|
|
public function __construct($interval = 15.) |
34
|
|
|
{ |
35
|
|
|
$this->_interval = $interval; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setClient(XmppClient $client) |
39
|
|
|
{ |
40
|
|
|
parent::setClient($client); |
41
|
|
|
|
42
|
|
|
$this->_client->on('state', [$this, 'enable'], \Kadet\Xmpp\Utils\filter\equals('ready')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Starts keep alive timer |
47
|
|
|
*/ |
48
|
|
|
public function enable() |
49
|
|
|
{ |
50
|
|
|
$this->_timer = $this->_client->connector->getLoop()->addPeriodicTimer($this->_interval, function() { |
51
|
|
|
$this->keepAlive(); |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Stops keep alive timer |
57
|
|
|
*/ |
58
|
|
|
public function disable() |
59
|
|
|
{ |
60
|
|
|
$this->_client->connector->getLoop()->cancelTimer($this->_timer); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function keepAlive() |
64
|
|
|
{ |
65
|
|
|
$ping = new Stanza('iq', ['type' => 'get', 'content' => [ |
66
|
|
|
new XmlElement('ping', 'urn:xmpp:ping') |
67
|
|
|
]]); |
68
|
|
|
|
69
|
|
|
$this->_client->write($ping); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.