@@ -25,10 +25,10 @@ class Client
2525 * This value SHOULD NOT be given unless you're sure you want to explicitly use a
2626 * given event loop instance.
2727 *
28- * @param ? LoopInterface $loop
29- * @param ? MulticastFactory $multicast
28+ * @param LoopInterface|null $loop
29+ * @param MulticastFactory|null $multicast
3030 */
31- public function __construct (LoopInterface $ loop = null , MulticastFactory $ multicast = null )
31+ public function __construct ($ loop = null , $ multicast = null )
3232 {
3333 $ this ->loop = $ loop ?: Loop::get ();
3434 $ this ->multicast = $ multicast ?: new MulticastFactory ($ this ->loop );
@@ -44,31 +44,48 @@ public function search($searchTarget = 'ssdp:all', $mx = 2)
4444 $ data .= "\r\n" ;
4545
4646 $ socket = $ this ->multicast ->createSender ();
47- // TODO: The TTL for the IP packet SHOULD default to 2 and SHOULD be configurable.
4847
49- $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , &$ deferred ) {
50- $ deferred ->resolve ();
51- $ socket ->close ();
52- });
48+ // TODO: The TTL for the IP packet SHOULD default to 2 and SHOULD be configurable.
5349
5450 $ 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 );
51+ $ timer = null ;
52+
53+ // Erstelle Deferred ohne Canceller-Parameter für bessere Kompatibilität
54+ $ deferred = new Deferred ();
55+
56+ // Füge die Cancellation-Logik separat hinzu
57+ $ promise = $ deferred ->promise ();
58+ if (method_exists ($ promise , 'cancel ' )) {
59+ // Für ReactPHP Promise v2
60+ $ promise = $ promise ->then (null , null , null , function () use (&$ socket , &$ timer , $ loop ) {
61+ // canceling resulting promise cancels timer and closes socket
62+ if ($ timer !== null ) {
63+ $ loop ->cancelTimer ($ timer );
64+ }
65+ $ socket ->close ();
66+ throw new RuntimeException ('Cancelled ' );
67+ });
68+ }
69+
70+ $ timer = $ this ->loop ->addTimer ($ mx , function () use ($ socket , $ deferred ) {
71+ $ deferred ->resolve ();
5872 $ socket ->close ();
59- throw new RuntimeException ('Cancelled ' );
6073 });
6174
6275 $ that = $ this ;
6376 $ socket ->on ('message ' , function ($ data , $ remote ) use ($ deferred , $ that ) {
6477 $ message = $ that ->parseMessage ($ data , $ remote );
65-
66- $ deferred ->progress ($ message );
78+ // React Promise v3+ doesn't support progress(), use notify() instead
79+ if (method_exists ($ deferred , 'progress ' )) {
80+ $ deferred ->progress ($ message );
81+ } else if (method_exists ($ deferred , 'notify ' )) {
82+ $ deferred ->notify ($ message );
83+ }
6784 });
6885
6986 $ socket ->send ($ data , self ::ADDRESS );
7087
71- return $ deferred -> promise () ;
88+ return $ promise ;
7289 }
7390
7491 /** @internal */
@@ -79,6 +96,7 @@ public function parseMessage($message, $remote)
7996 'data ' => $ message ,
8097 '_sender ' => $ remote
8198 );
99+
82100 return $ message ;
83101 }
84- }
102+ }
0 commit comments