@@ -28,7 +28,7 @@ class Client
2828 * @param ?LoopInterface $loop
2929 * @param ?MulticastFactory $multicast
3030 */
31- public function __construct (LoopInterface $ loop = null , MulticastFactory $ multicast = null )
31+ public function __construct (? LoopInterface $ loop = null , ? MulticastFactory $ multicast = null )
3232 {
3333 $ this ->loop = $ loop ?: Loop::get ();
3434 $ this ->multicast = $ multicast ?: new MulticastFactory ($ this ->loop );
@@ -46,24 +46,30 @@ public function search($searchTarget = 'ssdp:all', $mx = 2)
4646 $ socket = $ this ->multicast ->createSender ();
4747 // TODO: The TTL for the IP packet SHOULD default to 2 and SHOULD be configurable.
4848
49- $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , &$ deferred ) {
50- $ deferred ->resolve ();
49+ $ deferred = new Deferred (function () use (&$ socket , &$ timer , $ this ) {
50+ // canceling resulting promise cancels timer and closes socket
51+ if ($ timer !== null ) {
52+ $ this ->loop ->cancelTimer ($ timer );
53+ }
5154 $ socket ->close ();
55+ throw new RuntimeException ('Cancelled ' );
5256 });
5357
54- $ loop = $ this ->loop ;
55- $ deferred = new Deferred (function () use ($ socket , &$ timer , $ loop ) {
56- // canceling resulting promise cancels timer and closes socket
57- $ loop ->cancelTimer ($ timer );
58+ $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , $ deferred ) {
59+ $ deferred ->resolve ();
5860 $ socket ->close ();
59- throw new RuntimeException ('Cancelled ' );
6061 });
6162
6263 $ that = $ this ;
6364 $ socket ->on ('message ' , function ($ data , $ remote ) use ($ deferred , $ that ) {
6465 $ message = $ that ->parseMessage ($ data , $ remote );
6566
66- $ deferred ->progress ($ message );
67+ // React Promise v3+ doesn't support progress(), use emit() instead
68+ if (method_exists ($ deferred , 'progress ' )) {
69+ $ deferred ->progress ($ message );
70+ } else {
71+ $ deferred ->notify ($ message );
72+ }
6773 });
6874
6975 $ socket ->send ($ data , self ::ADDRESS );
0 commit comments