We’re using ReactPHP to establish a websocket connection and listen for some data. It works great as long as the websocket server is up and running.
If it can’t make the websocket connection, I need it to retry after a few seconds, but can’t get that far …
What I can’t figure out is how to get ReactPHP’s Socket Client to fail when it can’t connect (or the websocket server goes offline). I’ve tried things like the TimeoutConnector and what’s below, but no matter what happens, it just, well, does nothing, like it’s connected:
$connector = new ReactSocketConnector();
$connector->connect('localhost:1234')->then(
function (ConnectionInterface $connection) {
$connection->on('data', function ($data) use ($connection) {
... do all the things ...
});
$connection->on('end', function () use ($connection) {
echo 'end';
});
$connection->on('close', function () use ($connection) {
echo 'close';
});
$connection->on('error', function () use ($connection) {
echo 'error'
});
},
function (Exception $e) {
echo $e->getMessage();
}
);
Note that I can get the ‘end’ and ‘close’ events to fire after a successful connection, I just can’t get any events to fire if when starting the ReactPHP app the websocket server is offline and it can’t connect, just kinds of hangs there like its connected, ignoring any timeouts.
Thanks for any help or advice 🙂
2
Answers
This works great!
And then retry after 3 seconds ...
I already wrote an answer in https://github.com/orgs/reactphp/discussions/502, but I will also post the same answer here for better visibility.