1616use Thruway \Connection ;
1717use React \EventLoop \Factory as LoopFactory ;
1818use React \EventLoop \LoopInterface ;
19+ use Thruway \ClientSession ;
20+ use Tidal \WampWatch \Adapter \Thruway \ClientSession as Adapter ;
1921
2022trait CrossbarTestingTrait
2123{
@@ -24,6 +26,11 @@ trait CrossbarTestingTrait
2426 */
2527 private $ connection ;
2628
29+ /**
30+ * @var Adapter
31+ */
32+ private $ clientSession ;
33+
2734 /**
2835 * @var LoopInterface
2936 */
@@ -39,17 +46,29 @@ trait CrossbarTestingTrait
3946 */
4047 private $ monitoredSessionId = -2 ;
4148
49+ /**
50+ * @var string
51+ */
52+ private $ testTopicName = 'foo ' ;
53+
4254 private function setupConnection ()
4355 {
4456
4557 $ this ->clientSessionId = -1 ;
4658 $ this ->monitoredSessionId = -2 ;
4759
60+ $ this ->setupTestTopicName ();
61+
4862 Logger::set (new NullLogger ());
4963
50- $ this ->loop = LoopFactory::create ();
64+ $ this ->getConnection ()->on ('error ' , function ($ reason ) {
65+ echo "The connected has closed with error: {$ reason }\n" ;
66+ });
5167
52- $ this ->connection = $ this ->createConnection ($ this ->loop );
68+ $ this ->getConnection ()->on ('open ' , function (ClientSession $ session ) {
69+ $ this ->clientSession = $ this ->createSessionAdapter ($ session );
70+ $ this ->clientSessionId = $ session ->getSessionId ();
71+ });
5372
5473 }
5574
@@ -61,7 +80,7 @@ private function setupConnection()
6180 private function createConnection (LoopInterface $ loop = null )
6281 {
6382 if ($ loop === null ) {
64- $ loop = LoopFactory:: create ();
83+ $ loop = $ this -> getLoop ();
6584 }
6685
6786 return new Connection (
@@ -72,4 +91,62 @@ private function createConnection(LoopInterface $loop = null)
7291 $ loop
7392 );
7493 }
94+
95+ /**
96+ * @return \Thruway\Connection
97+ */
98+ private function createClientConnection ()
99+ {
100+ $ connection = $ this ->createConnection ();
101+
102+ $ connection ->on ('error ' , function ($ reason ) {
103+ echo "The client connection has closed with error: {$ reason }\n" ;
104+ });
105+ $ this ->getConnection ()->on ('close ' , [$ connection , 'close ' ]);
106+ $ connection ->on ('open ' , function ($ session ) {
107+ $ this ->clientSession = $ this ->createSessionAdapter ($ session );
108+ });
109+
110+ return $ connection ;
111+ }
112+
113+ private function setupTestTopicName ()
114+ {
115+ $ this ->testTopicName = "" ;
116+
117+ $ chars = str_split ("abcdefghijklmnopqrstuvwxyz " );
118+ for ($ i = 0 ; $ i < 10 ; $ i ++) {
119+ $ key = array_rand ($ chars );
120+ $ this ->testTopicName .= "" . $ chars [$ key ];
121+ }
122+ }
123+
124+ /**
125+ * @return \React\EventLoop\LoopInterface
126+ */
127+ private function getLoop ()
128+ {
129+ return $ this ->loop
130+ ?: $ this ->loop = LoopFactory::create ();
131+ }
132+
133+ /**
134+ * @param ClientSession $session
135+ *
136+ * @return Adapter
137+ */
138+ private function createSessionAdapter (ClientSession $ session )
139+ {
140+ return new Adapter ($ session );
141+ }
142+
143+ /**
144+ * @return \Thruway\Connection
145+ */
146+ public function getConnection ()
147+ {
148+ return $ this ->connection
149+ ?: $ this ->connection = $ this ->createConnection ();
150+ }
151+
75152}
0 commit comments