I'm developing an iOS app that has a chat system included (using these instructions). The api runs on a LAMP server, so I used ReactPhp instead of Twisted for python as socket handler to communicate between client/server and server/client.
This is the code I'm using for the file socket.php
<?
require 'vendor/autoload.php';
$port = 1337;
$host = '127.0.0.1';
$app = function ($request, $response) {
$response->writeHead(200, array('Content-Type' => 'text/plain'));
$response->end('Hello '.time()."
");
};
$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://'.$host.':'.$port."
";
$socket->listen($port, $host);
$loop->run();
?>
Then I just run this on the terminal:
php socket.php
The issues:
It works fine, but... if I close the terminal (or stop the process with ctrl+Z), the port stops listening. How do I get the socket to listen all the time from the moment Apache starts?
After closing the terminal (case 1), if I try running
php socket.php
again, I get this message: Could not bind to tcp://127.0.0.1:1337: Address already in use - If the port is already in use, then why am I unable to access it after closing the terminal?