I am rather new to ReactPHP but I am trying to achieve something that at first seems a bit tricky. I have set up a php script, which serves as a server, what is listening for connections through a socket. When a connection is estabilished, it is sending out a string to the connection. Let's call this server.php
, it looks as follows:
use React\Socket\ConnectionInterface;
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server('127.0.0.1:8080', $loop);
$socket->on('connection', function(ConnectionInterface $connection){
$connection->write('random string should come here');
});
echo "Listening on {$socket->getAddress()}\n";
$loop->run();
This file would run as a daemon on the server, in the background, waiting for connections so it can return the string to them. Now, what I want to achieve, is to write a client file, which connects to this "server", and waits, until it gets a response from him. When it gets the response, it should echo
out the response and then exit. (Close both the connection, and the file)
I presume I could do this, by creating something that I would call a fake loop, in which the client enters, and loops until the response is found, then exits the loop. But isn't there an easier way to achieve this?
I would need this because of the following scenario: My server will accept a password, which I will give hime at the time of initialization, with which it will unencrypt some API information. When someone connects to this server, which now has access to the API, it should return some data from the API it accesses. The visibility of the API could be made open, but no one else should be able to execute write commands on the API, and since I can't make separate keys to it, one for reading data (which should be public), and one for executing data(which shouldn't be public), this is the only scenario that I can imagine, which still keeps the writing of the data to the API secure.