I have two files opening a new socket and want them to connect to each other using React PHP. The following two files are the sockets:
First file test1.php
<?php
include 'vendor/autoload.php';
$socket = new \React\Socket\SocketServer('127.0.0.1:3030');
$socket->on('connection', function(\React\Socket\ConnectionInterface $connection) {
echo '[' . $connection->getRemoteAddress() . ' connected]' . PHP_EOL;
});
Second file test2.php
<?php
include 'vendor/autoload.php';
$socket = new \React\Socket\SocketServer('127.0.0.1:3031');
$connector = new \React\Socket\Connector();
$connector->connect('127.0.0.1:3030')
->then(function(\React\Socket\ConnectionInterface $connection) {
echo '[Connected with ' . $connection->getRemoteAddress() . ']' . PHP_EOL;
});
If I run php test1.php
and then php test2.php
I would expect the following outcome:
[Connected with tcp://127.0.0.1:3030]
[tcp://127.0.0.1:3031 connected]
However, the result is:
[Connected with tcp://127.0.0.1:3030]
[tcp://127.0.0.1:61594 connected]
What am I doing wrong here? How do I get React PHP to connect with the 3031 port?