NotionCommotion Posted November 5, 2016 Share Posted November 5, 2016 Messing around with http://reactphp.org/. Couple of questions... How can I access the server with any IP other than 127.0.0.1? How can I send data to it? What is different with this than just putting some script in an apache public directory? Kinda still missing the point of this. <?php require 'vendor/autoload.php'; $app = function ($request, $response) { $response->writeHead(200, array('Content-Type' => 'text/plain')); $response->end('$request=>'.json_encode($request).'$response=>'.json_encode($response)."DONE\n"); }; $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new React\Http\Server($socket, $loop); $http->on('request', $app); echo "Server running at http://127.0.0.1:1337\n"; $socket->listen(1337); $loop->run(); <?php function test($ip,$data=[]) { $options=[ CURLOPT_URL=>$ip, CURLOPT_RETURNTRANSFER=>true, CURLOPT_PORT=>1337, CURLOPT_POST=>1, CURLOPT_POSTFIELDS=>$data ]; $ch = curl_init(); curl_setopt_array( $ch, $options ); echo("\n\nTest for '$ip'\n"); echo var_dump(curl_exec( $ch )); } test('127.0.0.1'); test('127.0.0.1?x=123'); test('127.0.0.1',[1,2,3]); test('192.168.1.200'); //false test('192.168.1.201'); //false Quote Link to comment Share on other sites More sharing options...
requinix Posted November 5, 2016 Share Posted November 5, 2016 How can I access the server with any IP other than 127.0.0.1?Run it on another machine. How can I send data to it?Like POST? That's a cURL thing. Use the CURLOPT_POST and CURLOPT_POSTFIELDS options. What is different with this than just putting some script in an apache public directory? Kinda still missing the point of this.Having all connections handled within one PHP process. It's all in one script - don't need databases or shared memory or whatever to "remember" data across requests. And you can run each instance on its own port. I don't use it myself so I'm kinda just guessing there. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted November 6, 2016 Author Share Posted November 6, 2016 Thanks requinix, Yea, I tried it on another machine (or better yet, tried to access it from another machine), but it returns false. Oh yea, again. Not how do I send it data, but how do I receive it? As seen by my example, I tried those cURL options. Still need to better get my head about the whole asynchronous i/o thing. Quote Link to comment Share on other sites More sharing options...
requinix Posted November 6, 2016 Share Posted November 6, 2016 Yea, I tried it on another machine (or better yet, tried to access it from another machine), but it returns false.Check your firewall. Oh yea, again. Not how do I send it data, but how do I receive it? As seen by my example, I tried those cURL options.I imagine the data is within $request. Still need to better get my head about the whole asynchronous i/o thing.Again, not very familiar with React, but I wouldn't expect it to be truly asynchronous. Rather the code would be designed such that execution moves between different pieces of code quickly based on certain conditions - the same way Javascript isn't actually asynchronous but feels that way. Quote Link to comment Share on other sites More sharing options...
kicken Posted November 6, 2016 Share Posted November 6, 2016 How can I access the server with any IP other than 127.0.0.1? The server listens on the address specified listen call. Since you didn't specify an address it uses the default which is 127.0.0.1. You can instead specify the address you want it to listen on, or use the special address 0.0.0.0 to mean all addresses. If you specify a specific address it has to be one that exists on one of your network interfaces. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted November 6, 2016 Author Share Posted November 6, 2016 (edited) I too first thought the firewall, but no, however, I believe kicken's post addresses it. How can I specify the address? How did you know @kicken about this special 0.0.0.0 address? Is there any documentation? EDIT. Ah, from a depreciated tutorial https://blog.wyrihaximus.net/2014/04/the-reactphp-event-loop-explained-part-1-streams/, I think it is done using $socket->listen(13378, '0.0.0.0'); Does it make sense to run React on both a client and server at the same time? For instance, client initiates connection to server and waits for server to provide something, reacts to it, and repeats? How is the server made to return something (as it doesn't reply upon request but upon some event, right)? Which classes would I use for both sides? Maybe React\SocketClient\Connector for the client? Any example script would be much appreciated. Also, is it possible to have both a React server and Apache or Nginx server on the same machine which listen to the same port? It seems that either some sort of alias would be needed, or React would need to catch all requests and distribute as necessary. Thank you for your help. Edited November 6, 2016 by NotionCommotion Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted November 6, 2016 Author Share Posted November 6, 2016 Making a little progress. The following client will send the time to the server ever 5 seconds. When the server gets a request, it will respond with the same time value. My plan is to replace the time with some JSON which contains the task/data which is desired to be accomplished. Instead of having the server only respond when it gets a request from the client, I would like to have the server send data over based on some other event. I believe I can make the server send the data by using $conn->write($data);, and the client will receive this data on the $stream->on('data', function ($data) {} callback. So, how do I initiate the server to send the data? For instance, a web client makes a request to a machine running both Apache and this React server. The Apache script will authenticate, and then needs to somehow make the Request server send it out. Thanks! client.php <?php require '../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $dnsResolverFactory = new React\Dns\Resolver\Factory(); $dns = $dnsResolverFactory->createCached('8.8.8.8', $loop); $connector = new React\SocketClient\Connector($loop, $dns); $connector->create('127.0.0.1', 1337)->then(function (React\Stream\Stream $stream) use ($loop) { // 5 second loop to poll server $loop->addPeriodicTimer(5, function(React\EventLoop\Timer\Timer $timer) use (&$i, $loop, $stream) { // Do whatever you want $stream->write(time() . PHP_EOL); }); // Respond to data from server $stream->on('data', function ($data) { echo $data; }); }); $loop->run(); server.php <?php require '../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); // This event triggers every time a new connection comes in $socket->on('connection', function ($conn) { // Event listener for incoming data $conn->on('data', function ($data, $conn) { // Write data back to the connection $conn->write($data); // Echo the data into our terminal window echo $data; }); }); // Listen on port 1337 and IP 127.0.0.1 $socket->listen(1337, '127.0.0.1'); $loop->run(); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.