1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Pusher; |
4
|
|
|
|
5
|
|
|
use React\Dns\Resolver\Resolver; |
6
|
|
|
use React\EventLoop\LoopInterface; |
7
|
|
|
use Rx\DisposableInterface; |
8
|
|
|
use Rx\Observable; |
9
|
|
|
use Rx\ObserverInterface; |
10
|
|
|
use Rx\Subject\ReplaySubject; |
11
|
|
|
use Rx\Subject\Subject; |
12
|
|
|
use Rx\Websocket\Client; |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class WebSocket - WebSocket wrapper that queues messages while the connection is being established. |
16
|
|
|
*/ |
17
|
|
|
final class WebSocket extends Subject |
18
|
|
|
{ |
19
|
|
|
private $ws; |
20
|
|
|
private $sendSubject; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @internal |
24
|
|
|
* @param Observable $ws |
25
|
|
|
* @throws \InvalidArgumentException |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Observable $ws) |
28
|
|
|
{ |
29
|
|
|
$this->sendSubject = new ReplaySubject(); |
30
|
|
|
$this->ws = $ws; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function createFactory(string $url, bool $useMessageObject = false, array $subProtocols = [], LoopInterface $loop = null, Resolver $resolver = null): Subject |
34
|
|
|
{ |
35
|
|
|
return new self( |
36
|
|
|
new Client($url, $useMessageObject, $subProtocols, $loop, $resolver) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function onNext($value) |
41
|
|
|
{ |
42
|
|
|
$this->sendSubject->onNext($value); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function _subscribe(ObserverInterface $observer): DisposableInterface |
46
|
|
|
{ |
47
|
|
|
return $this->ws |
48
|
|
|
->do(function (Subject $ms) { |
49
|
|
|
// Replay buffered messages onto the MessageSubject |
50
|
|
|
$this->sendSubject->subscribe($ms); |
51
|
|
|
|
52
|
|
|
// Now that the connection has been established, use the message subject directly. |
53
|
|
|
$this->sendSubject = $ms; |
54
|
|
|
}) |
55
|
|
|
->finally(function () { |
56
|
|
|
// The connection has closed, so start buffering messages util it reconnects. |
57
|
|
|
$this->sendSubject = new ReplaySubject(); |
58
|
|
|
}) |
59
|
|
|
->switch() |
60
|
|
|
->repeatDelay() |
61
|
|
|
->subscribe($observer); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: