1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WyriHaximus\Pusher; |
5
|
|
|
|
6
|
|
|
use React\EventLoop\LoopInterface; |
7
|
|
|
use Rx\Disposable\CallbackDisposable; |
8
|
|
|
use Rx\Observable; |
9
|
|
|
use Rx\ObservableInterface; |
10
|
|
|
use Rx\Observer\CallbackObserver; |
11
|
|
|
use Rx\ObserverInterface; |
12
|
|
|
use Rx\React\Promise; |
13
|
|
|
use Rx\Scheduler\EventLoopScheduler; |
14
|
|
|
use Rx\SchedulerInterface; |
15
|
|
|
use Rx\Websocket\Client; |
16
|
|
|
use Rx\Websocket\MessageSubject; |
17
|
|
|
use WyriHaximus\ApiClient\Transport\Client as Transport; |
18
|
|
|
use WyriHaximus\ApiClient\Transport\Factory; |
19
|
|
|
use function React\Promise\resolve; |
20
|
|
|
use function EventLoop\getLoop; |
21
|
|
|
use function EventLoop\setLoop; |
22
|
|
|
|
23
|
|
|
class AsyncClient |
24
|
|
|
{ |
25
|
|
|
protected $transport; |
26
|
|
|
protected $app; |
27
|
|
|
protected $url; |
28
|
|
|
protected $client; |
29
|
|
|
protected $messages; |
30
|
|
|
protected $channels = []; |
31
|
|
|
|
32
|
|
|
public function __construct(LoopInterface $loop, string $app, Transport $transport = null) |
33
|
|
|
{ |
34
|
|
|
setLoop($loop); |
35
|
|
|
/*if (!($transport instanceof Transport)) { |
|
|
|
|
36
|
|
|
$transport = Factory::create($loop, [ |
37
|
|
|
'resource_namespace' => 'Async', |
38
|
|
|
] + ApiSettings::TRANSPORT_OPTIONS); |
39
|
|
|
} |
40
|
|
|
$this->transport = $transport;*/ |
41
|
|
|
|
42
|
|
|
$this->app = $app; |
43
|
|
|
|
44
|
|
|
$this->url = 'wss://ws.pusherapp.com/app/' . |
45
|
|
|
$this->app . |
46
|
|
|
'?client=wyrihaximus-php-pusher-client&version=0.0.1&protocol=7' |
47
|
|
|
; |
48
|
|
|
//Only create one connection and share the most recent among all subscriber |
49
|
|
|
$this->client = (new Client($this->url))->shareReplay(1); |
50
|
|
|
$this->messages = $this->client |
51
|
|
|
->flatMap(function (MessageSubject $ms) { |
52
|
|
|
return $ms; |
53
|
|
|
}) |
54
|
|
|
->map('json_decode'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function channel(string $channel): ObservableInterface |
58
|
|
|
{ |
59
|
|
|
if (isset($this->channels[$channel])) { |
60
|
|
|
return $this->channels[$channel]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$channelMessages = $this->messages->filter(function ($event) use ($channel) { |
64
|
|
|
return isset($event->channel) && $event->channel == $channel; |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
$events = Observable::create(function ( |
68
|
|
|
ObserverInterface $observer, |
69
|
|
|
SchedulerInterface $scheduler |
70
|
|
|
) use ( |
71
|
|
|
$channel, |
72
|
|
|
$channelMessages |
73
|
|
|
) { |
74
|
|
|
$subscription = $channelMessages |
75
|
|
|
->filter(function ($msg) { |
76
|
|
|
return $msg->event !== 'pusher_internal:subscription_succeeded'; |
77
|
|
|
}) |
78
|
|
|
->subscribe($observer, $scheduler); |
79
|
|
|
|
80
|
|
|
$this->send(['event' => 'pusher:subscribe', 'data' => ['channel' => $channel]]); |
81
|
|
|
|
82
|
|
|
return new CallbackDisposable(function () use ($channel, $subscription) { |
83
|
|
|
$this->send(['event' => 'pusher:unsubscribe', 'data' => ['channel' => $channel]]); |
84
|
|
|
$subscription->dispose(); |
85
|
|
|
}); |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
$this->channels[$channel] = $events->share(); |
89
|
|
|
return $this->channels[$channel]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function send(array $message) |
93
|
|
|
{ |
94
|
|
|
$this->client |
95
|
|
|
->take(1) |
96
|
|
|
->subscribeCallback(function (MessageSubject $ms) use ($message) { |
97
|
|
|
$ms->send(json_encode($message)); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.