1. Go to this page and download the library: Download react/socket 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 example snippets
$socket = new React\Socket\SocketServer('127.0.0.1:8080');
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
$connection->write("Hello " . $connection->getRemoteAddress() . "!\n");
$connection->write("Welcome to this amazing server!\n");
$connection->write("Here's a tip: don't say anything.\n");
$connection->on('data', function ($data) use ($connection) {
$connection->close();
});
});
$socket->pause();
Loop::addTimer(1.0, function () use ($socket) {
$socket->resume();
});
echo 'Shutting down server socket' . PHP_EOL;
$socket->close();
$socket = new React\Socket\SocketServer('127.0.0.1:8080');
$socket = new React\Socket\SocketServer('0.0.0.0:8080');
$socket = new React\Socket\SocketServer('[::1]:8080');
$socket = new React\Socket\SocketServer('127.0.0.1:0');
$address = $socket->getAddress();
$socket = new React\Socket\SocketServer('unix:///tmp/server.sock');
$socket = new React\Socket\SocketServer('php://fd/3');
// throws InvalidArgumentException due to missing port
$socket = new React\Socket\SocketServer('127.0.0.1');
$first = new React\Socket\SocketServer('127.0.0.1:8080');
// throws RuntimeException because port is already in use
$second = new React\Socket\SocketServer('127.0.0.1:8080');
$server = new React\Socket\UnixServer('/tmp/server.sock');
$first = new React\Socket\UnixServer('/tmp/same.sock');
// throws RuntimeException because socket is already in use
$second = new React\Socket\UnixServer('/tmp/same.sock');
$server = new React\Socket\LimitingServer($server, 100);
$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
$connection->write('hello there!' . PHP_EOL);
…
});
$server = new React\Socket\LimitingServer($server, 100);
$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
$connection->write('hello there!' . PHP_EOL);
…
});
$server = new React\Socket\LimitingServer($server, 100, true);
$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
$connection->write('hello there!' . PHP_EOL);
…
});
foreach ($server->getConnection() as $connection) {
$connection->write('Hi!');
}
$connector->connect('google.com:443')->then(
function (React\Socket\ConnectionInterface $connection) {
// connection successfully established
},
function (Exception $error) {
// failed to connect due to $error
}
);
$connector = new React\Socket\UnixConnector();
$connector->connect('/tmp/demo.sock')->then(function (React\Socket\ConnectionInterface $connection) {
$connection->write("HELLO\n");
});
$connector = new React\Socket\FixedUriConnector(
'unix:///var/run/docker.sock',
new React\Socket\UnixConnector()
);
// destination will be ignored, actually connects to Unix domain socket
$promise = $connector->connect('localhost:80');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.