swoole-增加监听的端口addListener使用

官方文档
https://wiki.swoole.com/#/server/methods?id=addlistener

例子:

<?php

$http = new Swoole\Http\Server("0.0.0.0", 9800);

$http->set([
    "worker_num" => 1
]);

$http->on("request", function ($request, $response) {
    $response->end("<h1> Hello swoole .#". rand(100, 999). "</h1>");
});
// 添加监听方法
$port = $http->addListener('127.0.0.1', 8000, SWOOLE_SOCK_TCP);
$port->set([
    'worker_num' => 2
]);
$port->on('receive', function (Swoole\Server $server, $fd, $reactor_id, $data) {
    echo "rece:". $fd, 'data:',$data;
    $server->send($fd, 'Swoole: '.$data);
    $server->close($fd);
});

echo  "http:127.0.0.0.1:9800\n";

$http->start();

?>

客户端:

<?php

$client = new swoole_client(SWOOLE_SOCK_TCP);

$client->connect('127.0.0.1', 8000);

$client->send('123123');

echo $client->recv(), "\n";

$client->close();

?>

posted @ 2021-01-20 22:02  洪先森  阅读(428)  评论(0编辑  收藏  举报