4343use WebSocket \Message \Message ;
4444use WebSocket \Middleware \MiddlewareInterface ;
4545use WebSocket \Trait \{
46+ ConfigurationTrait ,
4647 ListenerTrait ,
4748 LoggerAwareTrait ,
4849 SendMethodsTrait ,
5556 */
5657class Client implements LoggerAwareInterface, Stringable
5758{
59+ use ConfigurationTrait;
5860 /** @use ListenerTrait<Client> */
5961 use ListenerTrait;
6062 use LoggerAwareTrait;
@@ -91,7 +93,7 @@ class Client implements LoggerAwareInterface, Stringable
9193 /**
9294 * @param UriInterface|string $uri A ws/wss-URI
9395 */
94- public function __construct (UriInterface |string $ uri )
96+ public function __construct (UriInterface |string $ uri, Configuration | null $ configuration = null )
9597 {
9698 $ this ->socketUri = $ this ->parseUri ($ uri );
9799 $ this ->initLogger ();
@@ -102,6 +104,7 @@ public function __construct(UriInterface|string $uri)
102104 = $ this ->serverRequestFactory
103105 = $ this ->uriFactory
104106 = new DefaultHttpFactory ();
107+ $ this ->initConfiguration ($ configuration );
105108 }
106109
107110 /**
@@ -133,10 +136,7 @@ public function setStreamFactory(StreamFactory $streamFactory): self
133136 */
134137 public function setLogger (LoggerInterface $ logger ): void
135138 {
136- $ this ->logger = $ logger ;
137- if ($ this ->connection ) {
138- $ this ->connection ->setLogger ($ this ->logger );
139- }
139+ $ this ->getConfiguration ()->setLogger ($ logger );
140140 }
141141
142142 /**
@@ -317,12 +317,12 @@ public function start(int|float|null $timeout = null): void
317317 {
318318 // Check if running
319319 if ($ this ->running ) {
320- $ this ->logger ->warning ("[client] Client is already running " );
320+ $ this ->configuration -> getLogger () ->warning ("[client] Client is already running " );
321321 return ;
322322 }
323323 $ this ->running = true ;
324324 $ reconnect = false ;
325- $ this ->logger ->info ("[client] Client is running " );
325+ $ this ->configuration -> getLogger () ->info ("[client] Client is running " );
326326
327327 $ connection = $ this ->connection ();
328328
@@ -340,12 +340,12 @@ public function start(int|float|null $timeout = null): void
340340 $ this ->dispatch ($ message ->getOpcode (), [$ this , $ connection , $ message ]);
341341 } catch (MessageLevelInterface $ e ) {
342342 // Error, but keep connection open
343- $ this ->logger ->error ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
343+ $ this ->configuration -> getLogger () ->error ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
344344 $ this ->dispatch ('error ' , [$ this , $ connection , $ e ]);
345345 } catch (ConnectionLevelInterface $ e ) {
346346 // Error, disconnect connection
347347 $ this ->disconnect ();
348- $ this ->logger ->error ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
348+ $ this ->configuration -> getLogger () ->error ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
349349 $ this ->dispatch ('error ' , [$ this , $ connection , $ e ]);
350350 }
351351 }
@@ -357,7 +357,7 @@ public function start(int|float|null $timeout = null): void
357357 } catch (CloseException $ e ) {
358358 // Close connection
359359 $ connection ->close ($ e ->getCloseStatus (), $ e ->getMessage ());
360- $ this ->logger ->error ("[server] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
360+ $ this ->configuration -> getLogger () ->error ("[server] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
361361 $ this ->dispatch ('error ' , [$ this , $ connection , $ e ]);
362362 } catch (ReconnectException $ e ) {
363363 // Reconnect connection
@@ -366,21 +366,21 @@ public function start(int|float|null $timeout = null): void
366366 $ this ->socketUri = $ uri ;
367367 }
368368 $ connection ->close ();
369- $ this ->logger ->error ("[server] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
369+ $ this ->configuration -> getLogger () ->error ("[server] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
370370 $ this ->dispatch ('error ' , [$ this , $ connection , $ e ]);
371371 } catch (ExceptionInterface $ e ) {
372372 $ this ->disconnect ();
373373 $ this ->running = false ;
374374
375375 // Low-level error
376- $ this ->logger ->error ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
376+ $ this ->configuration -> getLogger () ->error ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
377377 $ this ->dispatch ('error ' , [$ this , null , $ e ]);
378378 } catch (Throwable $ e ) {
379379 $ this ->disconnect ();
380380 $ this ->running = false ;
381381
382382 // Crash it
383- $ this ->logger ->error ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
383+ $ this ->configuration -> getLogger () ->error ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
384384 throw $ e ;
385385 }
386386 gc_collect_cycles (); // Collect garbage
@@ -398,7 +398,7 @@ public function start(int|float|null $timeout = null): void
398398 public function stop (): void
399399 {
400400 $ this ->running = false ;
401- $ this ->logger ->info ("[client] Client is stopped " );
401+ $ this ->configuration -> getLogger () ->info ("[client] Client is stopped " );
402402 }
403403
404404 /**
@@ -468,7 +468,7 @@ public function connect(): void
468468 $ stream = $ client ->connect ();
469469 } catch (Throwable $ e ) {
470470 $ error = "Could not open socket to \"{$ hostUri }\": {$ e ->getMessage ()}" ;
471- $ this ->logger ->error ("[client] {$ error }" , ['exception ' => $ e ]);
471+ $ this ->configuration -> getLogger () ->error ("[client] {$ error }" , ['exception ' => $ e ]);
472472 throw new ClientException ($ error );
473473 }
474474 $ name = $ stream ->getRemoteName ();
@@ -484,14 +484,14 @@ public function connect(): void
484484 );
485485 $ this ->connection ->setFrameSize ($ this ->frameSize );
486486 $ this ->connection ->setTimeout ($ this ->timeout );
487- $ this ->connection ->setLogger ($ this ->logger );
487+ // $this->connection->setLogger($this->configuration->getLogger() );
488488 foreach ($ this ->middlewares as $ middleware ) {
489489 $ this ->connection ->addMiddleware ($ middleware );
490490 }
491491
492492 if (!$ this ->isConnected ()) {
493493 $ error = "Invalid stream on \"{$ hostUri }\". " ;
494- $ this ->logger ->error ("[client] {$ error }" );
494+ $ this ->configuration -> getLogger () ->error ("[client] {$ error }" );
495495 throw new ClientException ($ error );
496496 }
497497 try {
@@ -500,14 +500,14 @@ public function connect(): void
500500 $ response = $ this ->performHandshake ($ this ->socketUri , $ this ->connection );
501501 }
502502 } catch (ReconnectException $ e ) {
503- $ this ->logger ->info ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
503+ $ this ->configuration -> getLogger () ->info ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
504504 if ($ uri = $ e ->getUri ()) {
505505 $ this ->socketUri = $ uri ;
506506 }
507507 $ this ->connect ();
508508 return ;
509509 }
510- $ this ->logger ->info ("[client] Client connected to {$ this ->socketUri }" );
510+ $ this ->configuration -> getLogger () ->info ("[client] Client connected to {$ this ->socketUri }" );
511511 $ this ->dispatch ('handshake ' , [
512512 $ this ,
513513 $ this ->connection ,
@@ -524,7 +524,7 @@ public function disconnect(): void
524524 {
525525 if ($ this ->connection && $ this ->isConnected ()) {
526526 $ this ->connection ->disconnect ();
527- $ this ->logger ->info ('[client] Client disconnected ' );
527+ $ this ->configuration -> getLogger () ->info ('[client] Client disconnected ' );
528528 $ this ->dispatch ('disconnect ' , [$ this , $ this ->connection ]);
529529 }
530530 }
@@ -616,11 +616,11 @@ protected function performHandshake(Uri $uri, Connection $connection): ResponseI
616616 throw new HandshakeException ("Server sent bad upgrade response. " , $ response );
617617 }
618618 } catch (HandshakeException $ e ) {
619- $ this ->logger ->error ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
619+ $ this ->configuration -> getLogger () ->error ("[client] {$ e ->getMessage ()}" , ['exception ' => $ e ]);
620620 throw $ e ;
621621 }
622622
623- $ this ->logger ->debug ("[client] Handshake on {$ uri ->getPath ()}" );
623+ $ this ->configuration -> getLogger () ->debug ("[client] Handshake on {$ uri ->getPath ()}" );
624624 $ connection ->setHandshakeRequest ($ request );
625625 $ connection ->setHandshakeResponse ($ response );
626626
0 commit comments