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