1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WyriHaximus\Pusher; |
5
|
|
|
|
6
|
|
|
use Ratchet\Client\Connector; |
7
|
|
|
use Ratchet\Client\WebSocket; |
8
|
|
|
use Ratchet\RFC6455\Messaging\MessageInterface; |
9
|
|
|
use React\EventLoop\LoopInterface; |
10
|
|
|
use Rx\Observable; |
11
|
|
|
use Rx\ObservableInterface; |
12
|
|
|
use Rx\ObserverInterface; |
13
|
|
|
use Rx\React\Promise; |
14
|
|
|
use WyriHaximus\ApiClient\Transport\Client as Transport; |
15
|
|
|
use WyriHaximus\ApiClient\Transport\Factory; |
16
|
|
|
use function React\Promise\resolve; |
17
|
|
|
|
18
|
|
|
class AsyncClient |
19
|
|
|
{ |
20
|
|
|
protected $transport; |
21
|
|
|
protected $app; |
22
|
|
|
protected $url; |
23
|
|
|
protected $socket; |
24
|
|
|
protected $connection; |
25
|
|
|
protected $channels = []; |
26
|
|
|
|
27
|
|
|
public function __construct(LoopInterface $loop, string $app, Transport $transport = null) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/*if (!($transport instanceof Transport)) { |
|
|
|
|
30
|
|
|
$transport = Factory::create($loop, [ |
31
|
|
|
'resource_namespace' => 'Async', |
32
|
|
|
] + ApiSettings::TRANSPORT_OPTIONS); |
33
|
|
|
} |
34
|
|
|
$this->transport = $transport;*/ |
35
|
|
|
|
36
|
|
|
$this->app = $app; |
37
|
|
|
|
38
|
|
|
$this->url = 'wss://ws.pusherapp.com:443/app/' . |
39
|
|
|
$this->app . |
40
|
|
|
'?client=wyrihaximus-php-pusher-client&version=0.0.1&protocol=7' |
41
|
|
|
; |
42
|
|
|
$connector = new Connector($loop); |
43
|
|
|
$this->socket = $connector($this->url); |
44
|
|
|
$this->connection = Promise::toObservable($this->socket) |
45
|
|
|
->flatMap(function (WebSocket $socket) { |
46
|
|
|
$this->socket = resolve($socket); |
47
|
|
|
return Observable::create(function (ObserverInterface $observer) use ($socket) { |
48
|
|
|
$socket->on('message', function (MessageInterface $message) use ($observer) { |
49
|
|
|
$observer->onNext($message); |
50
|
|
|
}); |
51
|
|
|
$socket->on('close', function ($code, $reason) { |
|
|
|
|
52
|
|
|
|
53
|
|
|
}); |
54
|
|
|
}); |
55
|
|
|
})->map(function (MessageInterface $message) { |
56
|
|
|
return json_decode((string)$message, true); |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function subscribe(string $channel): ObservableInterface |
61
|
|
|
{ |
62
|
|
|
$this->channels[$channel] = $channel; |
63
|
|
|
$this->send([ |
64
|
|
|
'event' => 'pusher:subscribe', |
65
|
|
|
'data' => [ |
66
|
|
|
'channel' => $channel, |
67
|
|
|
], |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
return $this->connection->filter(function (array $event) use ($channel) { |
71
|
|
|
return isset($event['channel']) && $event['channel'] == $channel; |
72
|
|
|
})->filter(function (array $event) use ($channel) { |
73
|
|
|
return $event['event'] !== 'pusher_internal:subscription_succeeded'; |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function send(array $message) |
78
|
|
|
{ |
79
|
|
|
$this->socket->then(function (WebSocket $socket) use ($message) { |
80
|
|
|
$socket->send(json_encode($message)); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.