|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Voryx\RxWebsocket; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Ratchet\RFC6455\Handshake\ClientNegotiator; |
| 7 | +use React\Dns\Resolver\Factory; |
| 8 | +use React\HttpClient\Request; |
| 9 | +use React\HttpClient\Response; |
| 10 | +use Rx\Disposable\CallbackDisposable; |
| 11 | +use Rx\Observable\AnonymousObservable; |
| 12 | +use Rx\Observer\CallbackObserver; |
| 13 | +use Rx\ObserverInterface; |
| 14 | +use Rx\Subject\Subject; |
| 15 | + |
| 16 | +class Client extends Subject |
| 17 | +{ |
| 18 | + protected $url; |
| 19 | + |
| 20 | + /** |
| 21 | + * RxWebsocket constructor. |
| 22 | + * @param $url |
| 23 | + */ |
| 24 | + public function __construct($url) |
| 25 | + { |
| 26 | + $this->url = $url; |
| 27 | + } |
| 28 | + |
| 29 | + private function startConnection() |
| 30 | + { |
| 31 | + $loop = \EventLoop\getLoop(); |
| 32 | + |
| 33 | + $dnsResolverFactory = new Factory(); |
| 34 | + $dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop); |
| 35 | + |
| 36 | + $factory = new \React\HttpClient\Factory(); |
| 37 | + $client = $factory->create($loop, $dnsResolver); |
| 38 | + |
| 39 | + $cNegotiator = new ClientNegotiator($this->url); |
| 40 | + |
| 41 | + $headers = $cNegotiator->getRequest()->getHeaders(); |
| 42 | + |
| 43 | + $flatHeaders = []; |
| 44 | + foreach ($headers as $k => $v) { |
| 45 | + $flatHeaders[$k] = $v[0]; |
| 46 | + } |
| 47 | + |
| 48 | + $request = $client->request("GET", $this->url, $flatHeaders, '1.1'); |
| 49 | + |
| 50 | + $request->on('response', function (Response $response, Request $request) use ($cNegotiator) { |
| 51 | + if ($response->getCode() !== 101) { |
| 52 | + throw new \Exception("Unexpected response code " . $response->getCode()); |
| 53 | + } |
| 54 | + // TODO: Should validate response |
| 55 | + //$cNegotiator->validateResponse($response); |
| 56 | + |
| 57 | + parent::onNext(new MessageSubject( |
| 58 | + new AnonymousObservable(function (ObserverInterface $observer) use ($response) { |
| 59 | + $response->on('data', function ($data) use ($observer) { |
| 60 | + $observer->onNext($data); |
| 61 | + }); |
| 62 | + |
| 63 | + $response->on('error', function ($e) use ($observer) { |
| 64 | + $observer->onError($e); |
| 65 | + }); |
| 66 | + |
| 67 | + $response->on('close', function () use ($observer) { |
| 68 | + $observer->onCompleted(); |
| 69 | + }); |
| 70 | + |
| 71 | + $response->on('end', function () use ($observer) { |
| 72 | + $observer->onCompleted(); |
| 73 | + |
| 74 | + // complete the parent observer - we only do 1 connection |
| 75 | + parent::onCompleted(); |
| 76 | + }); |
| 77 | + |
| 78 | + |
| 79 | + return new CallbackDisposable(function () use ($response) { |
| 80 | + // commented this out because disposal was causing the other |
| 81 | + // end (the request) to close also - which causes the pending messages |
| 82 | + // to get tossed |
| 83 | + //$response->close(); |
| 84 | + }); |
| 85 | + }), |
| 86 | + new CallbackObserver( |
| 87 | + function ($x) use ($request) { |
| 88 | + $request->write($x); |
| 89 | + }, |
| 90 | + function ($e) use ($request) { |
| 91 | + $request->close(); |
| 92 | + }, |
| 93 | + function () use ($request) { |
| 94 | + $request->end(); |
| 95 | + } |
| 96 | + ), |
| 97 | + true |
| 98 | + )); |
| 99 | + }); |
| 100 | + |
| 101 | + $request->writeHead(); |
| 102 | + } |
| 103 | + |
| 104 | + public function subscribe(ObserverInterface $observer, $scheduler = null) |
| 105 | + { |
| 106 | + if (!$this->isStopped) { |
| 107 | + $this->startConnection(); |
| 108 | + } |
| 109 | + |
| 110 | + return parent::subscribe($observer, $scheduler); |
| 111 | + } |
| 112 | + |
| 113 | + public function send($value) |
| 114 | + { |
| 115 | + $this->onNext($value); |
| 116 | + } |
| 117 | + |
| 118 | + // Not sure we need this object to be a subject - just being an observer should be good enough I think |
| 119 | + public function onNext($value) |
| 120 | + { |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + public function onError(Exception $exception) |
| 125 | + { |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + public function onCompleted() |
| 130 | + { |
| 131 | + |
| 132 | + } |
| 133 | +} |
0 commit comments