PHP

PHP Socket框架的示例代码有哪些

小樊
83
2024-08-14 16:22:39
栏目: 编程语言

  1. ReactPHP
require 'vendor/autoload.php';

$loop = React\EventLoop\Factory::create();

$socket = new React\Socket\Server('127.0.0.1:8080', $loop);

$socket->on('connection', function ($conn) {
    $conn->on('data', function ($data) use ($conn) {
        $conn->write(strtoupper($data));
    });
});

$loop->run();
  1. Swoole
$server = new Swoole\Server("127.0.0.1", 9501);

$server->on('receive', function ($server, $fd, $from_id, $data) {
    $server->send($fd, strtoupper($data));
});

$server->start();
  1. Amp
require 'vendor/autoload.php';

use Amp\Socket\Server;
use Amp\Socket\Client;

Amp\Loop::run(function () {
    $server = new Server("127.0.0.1:8080");

    $server->on('connection', function (Client $client) {
        $client->on('data', function ($data) use ($client) {
            $client->write(strtoupper($data));
        });
    });
});

0
看了该问题的人还看了