swoole tcp&udp

tcp服务端

php 复制代码
use Swoole\Server;

$server = new Server('127.0.0.1', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

$server->on('Connect', function (Server $server, int $fd, int $reactorId) {
    echo "Client: Connect fd:" . $fd . "reactorId :" . $reactorId . ".Connect.\n";
});

$server->on('Receive', function (Server $server, int $fd, int $reactorId, $data) {
    echo "Client: Receive fd:" . $fd . "reactorId :" . $reactorId . ".Connect.\n";
    echo "data:" . $data . "\n";
    $msg = "hello fd:" . $fd;
    $server->send($fd, $msg);
});

$server->on('Close', function (Server $server, int $fd, int $reactorId) {
    echo "Client: Receive fd:" . $fd . "reactorId :" . $reactorId . ".Connect.\n";
});

$server->start();

udp服务端

php 复制代码
use Swoole\Server;

$server = new Server('127.0.0.1', 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);

$server->on('Connect', function (Server $server, int $fd, int $reactorId) {
    echo "Client: Connect fd:" . $fd . "reactorId :" . $reactorId . ".Connect.\n";
});

$server->on('Packet', function (Server $server, string $data, array $clientInfo) {
    var_dump($clientInfo);
    $server->sendto($clientInfo['address'], $clientInfo['port'], "Server:{$data}");
});

$server->on('Close', function (Server $server, int $fd, int $reactorId) {
    echo "Client: Receive fd:" . $fd . "reactorId :" . $reactorId . ".Connect.\n";
});

$server->start();

tcp 同步堵塞 客户端

php 复制代码
use Swoole\Client;

$host = '127.0.0.1';
$part = 9501;

$client = new Client(SWOOLE_SOCK_TCP);
if (!$client->connect($host, $part, 0.5)) {
    echo "connect failed. Error: {$client->errCode}\n";
    $client->close();
    //重试
    $client->connect($host, $part);
}
$client->send("hello world\n");
echo $client->recv() . "\n";
$client->close();

udp 同步堵塞 客户端

php 复制代码
use Swoole\Client;

$host = '127.0.0.1';
$part = 9502;

$client = new Client(SWOOLE_SOCK_UDP);
if (!$client->connect($host, $part, 0.5)) {
    echo "connect failed. Error: {$client->errCode}\n";
    $client->close();
    //重试
    $client->connect($host, $part);
}
$client->send("hello world\n");
echo $client->recv() . "\n";
$client->close();

tcp 异步回调 客户端

php 复制代码
use Swoole\Async\Client;

$host = '127.0.0.1';
$part = 9501;

function doConnect(Client $client) {
    if ($client->isConnected()) {
        $client->send("hello word");
    }
}
function onReceive(Client $client, string $data) {
    $recv_len = strlen($data);
    echo $data . "\n";
    $client->send("is test");
    $client->close();
    if (!$client->isConnected()) {
        echo "closed\n";
    }
}

$client = new Client(SWOOLE_SOCK_TCP);

$client->on("connect", "doConnect");//函数方法回调

$client->on("receive", "onReceive");

$client->on("error", function (Client $client) {
    echo "error\n";
});

$client->on("close", function (Client $client) {
    echo "close\n";
});

$client->connect($host, $part, 0.2);

udp 异步方法回调 客户端

php 复制代码
use Swoole\Async\Client;

$host = '127.0.0.1';
$part = 9502;

class UserCliten {
    public static function onConnect(Client $client) {
        if ($client->isConnected()) {
            $client->send("hello word");
        }

    }
    public function onReceive(Client $client, string $data) {
        $recv_len = strlen($data);
        echo $data . "\n";
        $client->send("is test");
        $client->close();
        if (!$client->isConnected()) {
            echo "closed\n";
        }
    }
}

$user_client = new UserCliten();

$client = new Client(SWOOLE_SOCK_UDP);

$client->on("connect", "UserCliten::onConnect");//类方法回调

$client->on("receive", [$user_client, 'onReceive']);//对象方法回调

$client->on("error", function (Client $client) {
    echo "error\n";
});

$client->on("close", function (Client $client) {
    echo "close\n";
});

$client->connect($host, $part, 0.2);
相关推荐
1二山似5 天前
crmeb多商户启动swoole时报‘加密文件丢失’
后端·swoole
沟通QQ:4877392782 个月前
使用Matlab基于A星算法的多任务多AGV路径规划系统优化
swoole
stark张宇3 个月前
高手项目:手把手带你构建AI办公应用,赋能企业数字化
ai编程·laravel·swoole
dacheng-gao3 个月前
Yii2-Swoole 快速入门
swoole·yii2
00后程序员张3 个月前
Swoole HTTPS 实战,在生产环境部署、性能权衡与排查流程
后端·ios·小程序·https·uni-app·iphone·swoole
myloe003 个月前
Linux系统下如何彻底卸载软件五种方法详解
swoole
欧的曼5 个月前
cygwin+php教程(swoole扩展+redis扩展)
开发语言·redis·后端·mysql·nginx·php·swoole
iFulling6 个月前
【PHP】Swoole:CentOS安装Composer+Hyperf
centos·php·swoole
007php0076 个月前
使用LNMP一键安装包安装PHP、Nginx、Redis、Swoole、OPcache
java·开发语言·redis·python·nginx·php·swoole