Ratchet IoServer and Client in the same script?

143 views
Skip to first unread message

hunter...@gmail.com

unread,
Apr 8, 2014, 1:57:52 PM4/8/14
to ratch...@googlegroups.com
Hi to all,
sorry if my question can seems stupid but I'm not so much expert in Ratchet and a little bit newbie with it.
I already have a Ratchet IoServer running that serve some clients (calling it Server 1 from now on it listen on port 8082 for clients to connect).
What I need is to let the Server 1 to connect as a client too to another Ratchet IoSever (calling Server 2 from now on it listen on port 8182 for Server 1 to connect).
Server 1 should send messages coming from its clients to Server 2 and/or listen for messages coming from Server 2 and deliver to its clients connected directly to it (Server 1).

Now I'm trying to do some tests with something like:

****** Server 1 Client Part Start **********

$loop_client = React\EventLoop\Factory::create();

$client = stream_socket_client('tcp://127.0.0.1:8182');
$conn_client = new React\Socket\Connection($client, $loop_client);
$conn_client->pipe(new React\Stream\Stream(STDOUT, $loop_client));
$conn_client->write("Hello World!\n");

$loop_client->run();

******* Server 1 Client Part Stop ***********


****** Server 1 Server Part Start **********

$app=new App();
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$socket->listen('8082', '127.0.0.1');

$server = new IoServer(
                       $app,
                       $socket,
                       $loop
                       );

$server->run();

******* Server 1 Server Part Stop ***********

My problem is that the script never reach the server part , I think because the client $loop_client->run() part is blocking.
If i move it after the $server->run() the server run correctly, the connection to Server 2 is made but the message "Hello World" is never received on the Server 2, I think because the  $server->run() is blocking.

Can you help me to reach this goal?
Thank you in advance,
Bye Bye
Marcello




tim...@gmail.com

unread,
Apr 15, 2014, 8:48:36 PM4/15/14
to ratch...@googlegroups.com, hunter...@gmail.com
You are correct on it never reaching the server part... once you hit run() no code after it will execute until the event loop ends

cboden

unread,
Apr 19, 2014, 11:22:05 AM4/19/14
to ratch...@googlegroups.com, hunter...@gmail.com
As Tim said, once you execute run() it grabs execution of your script. Only create one $loop and re-use it with both applications and then run() that one loop. Events on both sockets will be handled by the single loop and delegated appropriately. 

tim...@gmail.com

unread,
Apr 24, 2014, 10:59:31 AM4/24/14
to ratch...@googlegroups.com, hunter...@gmail.com
What is the proper way to use the http client, sharing the event loop, AFTER calling run().. 
trying to impliment and auth handler that does async http call to respond to an rpc call.. 

so far everything i have done has caused the event loop to end after the http-client call completes..  

hunter...@gmail.com

unread,
Apr 24, 2014, 12:08:24 PM4/24/14
to ratch...@googlegroups.com, hunter...@gmail.com

Thank you very much for your help.
I done it and it works...
Thank you again
Bye to all
Reply all
Reply to author
Forward
0 new messages