@@ -81,6 +81,11 @@ class Client
8181 */
8282 protected $ callbacks = [];
8383
84+ /**
85+ * @var bool
86+ */
87+ protected $ async = false ;
88+
8489 /**
8590 * Client constructor.
8691 * @param $uri
@@ -111,6 +116,7 @@ public function createRequest($url, $async = false, $keep = false)
111116 $ this ->scheme = isset ($ info ['scheme ' ]) ? $ info ['scheme ' ] : 'http ' ;
112117 $ this ->host = $ info ['host ' ];
113118 $ this ->port = isset ($ info ['port ' ]) ? $ info ['port ' ] : 80 ;
119+ $ this ->async = $ async ;
114120
115121 switch ($ this ->scheme ) {
116122 case 'tcp ' :
@@ -307,6 +313,14 @@ public function onError(swoole_client $client) {}
307313 */
308314 public function onClose (swoole_client $ client ) {}
309315
316+ /**
317+ * @return mixed
318+ */
319+ public function connect ()
320+ {
321+ return $ this ->client ->connect ($ this ->host , $ this ->port , $ this ->timeout );
322+ }
323+
310324 /**
311325 * @return bool
312326 */
@@ -341,18 +355,18 @@ public function send($data = '')
341355 if (null === $ this ->client ) {
342356 throw new LogicException ('Please call the createRequest method first ' );
343357 }
344- if ( ! $ this ->client -> connect ($ this -> host , $ this -> port , $ this -> timeout )) {
358+ if ( ! $ this ->connect ()) {
345359 throw new RuntimeException (socket_strerror ($ this ->client ->errCode ));
346360 }
361+ }
347362
348- $ this ->client ->send ($ this ->wrapBody ($ data ));
349- $ receive = $ this ->receive ();
350- } else {
351- $ this ->client ->send ($ this ->wrapBody ($ data ));
352- $ receive = $ this ->receive ();
363+ $ this ->client ->send ($ this ->wrapBody ($ data ));
364+
365+ if (!$ this ->async ) {
366+ return $ this ->receive ();
353367 }
354368
355- return $ receive ;
369+ return true ;
356370 }
357371
358372 /**
@@ -363,7 +377,7 @@ public function start()
363377 foreach ($ this ->callbacks as $ event => $ callback ) {
364378 $ this ->client ->on ($ event , $ callback );
365379 }
366- $ this ->client -> connect ($ this -> host , $ this -> port , $ this -> timeout );
380+ $ this ->connect ();
367381 }
368382
369383 public function __destruct ()
0 commit comments