PHP code example of react / socket-client
1. Go to this page and download the library: Download react/socket-client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
react / socket-client example snippets
// old from SocketClient component and namespace
$connector = new React\SocketClient\Connector($loop);
$connector->connect('google.com:80')->then(function (ConnectionInterface $conn) {
$connection->write('…');
});
// new
$connector = new React\Socket\Connector($loop);
$connector->connect('google.com:80')->then(function (ConnectionInterface $conn) {
$connection->write('…');
});
$connector->connect('google.com:443')->then(
function (ConnectionInterface $connection) {
// connection successfully established
},
function (Exception $error) {
// failed to connect due to $error
}
);
$promise = $connector->connect($uri);
$promise->cancel();
$connection->on('data', function ($chunk) {
echo $chunk;
});
$connection->on('end', function () {
echo 'ended';
});
$connection->on('error', function (Exception $e) {
echo 'error: ' . $e->getMessage();
});
$connection->on('close', function () {
echo 'closed';
});
$connection->write($data);
$connection->end($data = null);
$connection->close();
// …
$address = $connection->getRemoteAddress();
echo 'Connected to ' . $address . PHP_EOL;
$address = $connection->getRemoteAddress();
$ip = trim(parse_url('tcp://' . $address, PHP_URL_HOST), '[]');
echo 'Connected to ' . $ip . PHP_EOL;
$address = $connection->getLocalAddress();
echo 'Connected via ' . $address . PHP_EOL;
$loop = React\EventLoop\Factory::create();
$connector = new Connector($loop);
$connector->connect($uri)->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
$loop->run();
$connector->connect('www.google.com:80')->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
$connector->connect('tls://www.google.com:443')->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
$connector->connect('unix:///tmp/demo.sock')->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
$connector = new Connector($loop, array(
'dns' => '127.0.1.1'
));
$connector->connect('localhost:80')->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
$connector = new Connector($loop, array(
'dns' => false
));
$connector->connect('127.0.0.1:80')->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$resolver = $dnsResolverFactory->createCached('127.0.1.1', $loop);
$connector = new Connector($loop, array(
'dns' => $resolver
));
$connector->connect('localhost:80')->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
$connector = new Connector($loop, array(
'timeout' => 10.0
));
$connector = new Connector($loop, array(
'timeout' => false
));
// only allow secure TLS connections
$connector = new Connector($loop, array(
'tcp' => false,
'tls' => true,
'unix' => false,
));
$connector->connect('tls://google.com:443')->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
// allow insecure TLS connections
$connector = new Connector($loop, array(
'tcp' => array(
'bindto' => '192.168.0.1:0'
),
'tls' => array(
'verify_peer' => false,
'verify_peer_name' => false
),
));
$connector->connect('tls://localhost:443')->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$resolver = $dnsResolverFactory->createCached('127.0.1.1', $loop);
$tcp = new DnsConnector(new TcpConnector($loop), $resolver);
$tls = new SecureConnector($tcp, $loop);
$unix = new UnixConnector($loop);
$connector = new Connector($loop, array(
'tcp' => $tcp,
'tls' => $tls,
'unix' => $unix,
'dns' => false,
'timeout' => false,
));
$connector->connect('google.com:80')->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
$tcpConnector = new React\SocketClient\TcpConnector($loop);
$tcpConnector->connect('127.0.0.1:80')->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
$loop->run();
$promise = $tcpConnector->connect('127.0.0.1:80');
$promise->cancel();
$tcpConnector = new React\SocketClient\TcpConnector($loop, array(
'bindto' => '192.168.0.1:0'
));
$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dns = $dnsResolverFactory->createCached('8.8.8.8', $loop);
$dnsConnector = new React\SocketClient\DnsConnector($tcpConnector, $dns);
$dnsConnector->connect('www.google.com:80')->then(function (ConnectionInterface $connection) {
$connection->write('...');
$connection->end();
});
$loop->run();
$promise = $dnsConnector->connect('www.google.com:80');
$promise->cancel();
$secureConnector = new React\SocketClient\SecureConnector($dnsConnector, $loop);
$secureConnector->connect('www.google.com:443')->then(function (ConnectionInterface $connection) {
$connection->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
...
});
$loop->run();
$promise = $secureConnector->connect('www.google.com:443');
$promise->cancel();
$secureConnector = new React\SocketClient\SecureConnector($dnsConnector, $loop, array(
'verify_peer' => false,
'verify_peer_name' => false
));
$timeoutConnector = new React\SocketClient\TimeoutConnector($connector, 3.0, $loop);
$timeoutConnector->connect('google.com:80')->then(function (ConnectionInterface $connection) {
// connection succeeded within 3.0 seconds
});
$promise = $timeoutConnector->connect('google.com:80');
$promise->cancel();
$connector = new React\SocketClient\UnixConnector($loop);
$connector->connect('/tmp/demo.sock')->then(function (ConnectionInterface $connection) {
$connection->write("HELLO\n");
});
$loop->run();