1. Go to this page and download the library: Download icicleio/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/ */
icicleio / socket example snippets
use Icicle\Coroutine\Coroutine;
use Icicle\Loop;
use Icicle\Socket\Server\DefaultServerFactory;
use Icicle\Socket\Server\Server;
use Icicle\Socket\Socket;
$server = (new DefaultServerFactory())->create('localhost', 8080);
$generator = function (Server $server) {
printf("Server listening on %s:%d\n", $server->getAddress(), $server->getPort());
$generator = function (Socket $socket) {
$request = '';
do {
$request .= (yield $socket->read(0, "\n"));
} while (substr($request, -4) !== "\r\n\r\n");
$message = sprintf("Received the following request:\r\n\r\n%s", $request);
$data = "HTTP/1.1 200 OK\r\n";
$data .= "Content-Type: text/plain\r\n";
$data .= sprintf("Content-Length: %d\r\n", strlen($message));
$data .= "Connection: close\r\n";
$data .= "\r\n";
$data .= $message;
yield $socket->write($data);
$socket->close();
};
while ($server->isOpen()) {
// Handle client in a separate coroutine so this coroutine is not blocked.
$coroutine = new Coroutine($generator(yield $server->accept()));
$coroutine->done(null, function ($exception) {
printf("Client error: %s\n", $exception->getMessage());
});
}
};
$coroutine = new Coroutine($generator($server));
$coroutine->done();
Loop\run();