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 RuntimeException; |
8
|
|
|
use Rx\Observable; |
9
|
|
|
use Rx\Scheduler; |
10
|
|
|
use Rx\Subject\Subject; |
11
|
|
|
use Rx\Websocket\WebsocketErrorException; |
12
|
|
|
use Throwable; |
13
|
|
|
|
14
|
|
|
final class AsyncClient |
15
|
|
|
{ |
16
|
|
|
const DEFAULT_DELAY = 200; |
17
|
|
|
const NO_ACTIVITY_TIMEOUT = 120; |
18
|
|
|
const NO_PING_RESPONSE_TIMEOUT = 30; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Observable |
22
|
|
|
*/ |
23
|
|
|
private $messages; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $channels = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $delay = self::DEFAULT_DELAY; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Subject |
37
|
|
|
*/ |
38
|
|
|
private $client; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Observable |
42
|
|
|
*/ |
43
|
|
|
private $connected; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @internal |
47
|
|
|
* @param Subject $client |
48
|
|
|
* @throws \InvalidArgumentException |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Subject $client) |
51
|
|
|
{ |
52
|
|
|
$this->client = $client; |
53
|
|
|
|
54
|
|
|
/** @var Observable $events */ |
55
|
5 |
|
$events = $client |
56
|
|
|
->_ApiClients_jsonDecode() |
57
|
5 |
|
->map([Event::class, 'createFromMessage']) |
58
|
|
|
->singleInstance(); |
59
|
|
|
|
60
|
|
|
$pusherErrors = $events |
61
|
|
|
->filter([Event::class, 'isError']) |
62
|
|
|
->flatMap(function (Event $event) { |
63
|
|
|
$throwable = new PusherErrorException($event->getData()['message'], (int)$event->getData()['code']); |
64
|
|
|
|
65
|
|
|
return Observable::error($throwable); |
66
|
5 |
|
}); |
67
|
|
|
|
68
|
|
|
$this->connected = $events |
69
|
|
|
->filter([Event::class, 'connectionEstablished']) |
70
|
|
|
->singleInstance(); |
71
|
2 |
|
|
72
|
5 |
|
$this->messages = $events |
73
|
|
|
->merge($this->timeout($events)) |
74
|
|
|
->merge($pusherErrors) |
75
|
|
|
->retryWhen(function (Observable $errors) { |
76
|
5 |
|
return $errors->flatMap(function (Throwable $throwable) { |
77
|
|
|
return $this->handleLowLevelError($throwable); |
78
|
|
|
}); |
79
|
|
|
}) |
80
|
|
|
->singleInstance(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param LoopInterface $loop |
85
|
|
|
* @param string $app Application ID |
86
|
|
|
* @param Resolver $resolver Optional DNS resolver |
87
|
|
|
* @throws \InvalidArgumentException |
88
|
|
|
* @return AsyncClient |
89
|
|
|
*/ |
90
|
|
|
public static function create(LoopInterface $loop, string $app, Resolver $resolver = null): AsyncClient |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
5 |
|
Scheduler::setDefaultFactory(function () use ($loop) { |
94
|
|
|
return new Scheduler\EventLoopScheduler($loop); |
|
|
|
|
95
|
|
|
}); |
96
|
5 |
|
} catch (Throwable $t) { |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return new self( |
100
|
|
|
new Websocket(ApiSettings::createUrl($app), false, [], $loop, $resolver) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Listen on a channel. |
106
|
|
|
* |
107
|
|
|
* @param string $channel Channel to listen on |
108
|
|
|
* @throws \InvalidArgumentException |
109
|
|
|
* @return Observable |
110
|
|
|
*/ |
111
|
|
|
public function channel(string $channel): Observable |
112
|
|
|
{ |
113
|
|
|
// Only join a channel once |
114
|
|
|
if (isset($this->channels[$channel])) { |
115
|
|
|
return $this->channels[$channel]; |
116
|
5 |
|
} |
117
|
|
|
|
118
|
|
|
// Ensure we only get messages for the given channel |
119
|
|
|
$channelMessages = $this->messages->filter(function (Event $event) use ($channel) { |
120
|
|
|
return $event->getChannel() !== '' && $event->getChannel() === $channel; |
121
|
2 |
|
}); |
122
|
2 |
|
|
123
|
5 |
|
$subscribe = $this->connected |
124
|
|
|
->do(function () use ($channel) { |
125
|
|
|
// Subscribe to pusher channel after connected |
126
|
5 |
|
$this->send(Event::subscribeOn($channel)); |
127
|
5 |
|
}) |
128
|
|
|
->flatMapTo(Observable::empty()); |
129
|
|
|
|
130
|
|
|
// Observable representing channel events |
131
|
|
|
$events = $channelMessages |
132
|
|
|
->merge($subscribe) |
133
|
|
|
->filter([Event::class, 'subscriptionSucceeded']) |
134
|
|
|
->finally(function () use ($channel) { |
135
|
4 |
|
// Send unsubscribe event |
136
|
|
|
$this->send(Event::unsubscribeOn($channel)); |
137
|
|
|
|
138
|
|
|
// Remove our channel from the channel list so we don't resubscribe in case we reconnect |
139
|
|
|
unset($this->channels[$channel]); |
140
|
1 |
|
}); |
141
|
4 |
|
|
142
|
3 |
|
$this->channels[$channel] = $events; |
143
|
|
|
|
144
|
|
|
return $this->channels[$channel]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
4 |
|
* Send a message through the client. |
149
|
|
|
* |
150
|
|
|
* @param array $message Message to send, will be json encoded |
151
|
|
|
* |
152
|
4 |
|
*/ |
153
|
4 |
|
public function send(array $message) |
154
|
4 |
|
{ |
155
|
4 |
|
$this->client->onNext(json_encode($message)); |
156
|
4 |
|
} |
157
|
4 |
|
|
158
|
4 |
|
/** |
159
|
|
|
* Returns an observable of TimeoutException. |
160
|
|
|
* The timeout observable will get cancelled every time a new event is received. |
161
|
|
|
* |
162
|
|
|
* @param Observable $events |
163
|
|
|
* @return Observable |
164
|
|
|
*/ |
165
|
|
|
public function timeout(Observable $events): Observable |
166
|
|
|
{ |
167
|
|
|
$timeoutDuration = $this->connected->map(function (Event $event) { |
168
|
|
|
return ($event->getData()['activity_timeout'] ?? self::NO_ACTIVITY_TIMEOUT) * 1000; |
169
|
2 |
|
}); |
170
|
|
|
|
171
|
|
|
return $timeoutDuration |
172
|
2 |
|
->combineLatest([$events]) |
173
|
|
|
->pluck(0) |
174
|
|
|
->concat(Observable::of(-1)) |
175
|
|
|
->flatMapLatest(function (int $time) { |
176
|
|
|
|
177
|
|
|
// If the events observable ends, return an empty observable so we don't keep the stream alive |
178
|
|
|
if ($time === -1) { |
179
|
2 |
|
return Observable::empty(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return Observable::never() |
183
|
|
|
->timeout($time) |
184
|
|
|
->catch(function () use ($time) { |
185
|
2 |
|
// ping (do something that causes incoming stream to get a message) |
186
|
2 |
|
$this->send(Event::ping()); |
187
|
|
|
// this timeout will actually timeout with a TimeoutException - causing |
188
|
|
|
// everything above this to dispose |
189
|
|
|
return Observable::never()->timeout($time); |
190
|
|
|
}); |
191
|
|
|
}); |
192
|
2 |
|
} |
193
|
2 |
|
|
194
|
|
|
/** |
195
|
2 |
|
* Handle errors as described at https://pusher.com/docs/pusher_protocol#error-codes. |
196
|
|
|
* @param Throwable $throwable |
197
|
2 |
|
* @return Observable |
198
|
|
|
*/ |
199
|
|
|
private function handleLowLevelError(Throwable $throwable): Observable |
200
|
|
|
{ |
201
|
|
|
// Only allow certain, relevant, exceptions |
202
|
|
|
if (!($throwable instanceof WebsocketErrorException) && |
203
|
|
|
!($throwable instanceof RuntimeException) && |
204
|
2 |
|
!($throwable instanceof PusherErrorException) |
205
|
2 |
|
) { |
206
|
|
|
return Observable::error($throwable); |
207
|
|
|
} |
208
|
2 |
|
|
209
|
|
|
$code = $throwable->getCode(); |
210
|
2 |
|
$pusherError = ($throwable instanceof WebsocketErrorException || $throwable instanceof PusherErrorException); |
211
|
|
|
|
212
|
|
|
// Errors 4000-4099, don't retry connecting |
213
|
|
|
if ($pusherError && $code >= 4000 && $code <= 4099) { |
214
|
|
|
return Observable::error($throwable); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// Errors 4100-4199 reconnect after 1 or more seconds, we do it after 1.001 second |
218
|
|
|
if ($pusherError && $code >= 4100 && $code <= 4199) { |
219
|
|
|
return Observable::timer(1001); |
220
|
|
|
} |
221
|
2 |
|
|
222
|
|
|
// Errors 4200-4299 connection closed by Pusher, reconnect immediately, we wait 0.001 second |
223
|
|
|
if ($pusherError && $code >= 4200 && $code <= 4299) { |
224
|
2 |
|
return Observable::timer(1); |
225
|
2 |
|
} |
226
|
|
|
|
227
|
|
|
// Double our delay each time we get here |
228
|
|
|
$this->delay *= 2; |
229
|
|
|
|
230
|
|
|
return Observable::timer($this->delay); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: