• windows 搭建 swoole开发环境(官网已支持)


    第一步下载:swoole官网下载 swoole-cli-v5.0.3-cygwin-x64.zip 只支持 64 位的系统

    第二步解压到指定文件夹:E:\phpstudy_pro\WWW\swoole-cli-v5.0.3-cygwin-x64

    第三步设置环境变量:把解压后的文件夹下的 bin 目录路径配置到系统的 Path 环境变量中,确定保存

     

    第四步检查安装情况:打开CMD命令行输入:swoole-cli -v,安装成功

    第五步:编写简单的TCP服务器代码:TCP.php

    1. 服务端:

    1. class TCP
    2. {
    3. private $server = null;
    4. public function __construct()
    5. {
    6. $this->server = new Swoole\Server('127.0.0.1', 9501);
    7. $this->server->set(array(
    8. 'worker_num' => 4, // 进程数
    9. 'max_request' => 50, // 每个进程最大接受请求数
    10. ));
    11. //监听连接进入事件。
    12. $this->server->on('Connect', [$this, 'onConnect']);
    13. //监听数据接收事件。
    14. $this->server->on('Receive', [$this, 'onReceive']);
    15. 监听连接关闭事件。
    16. $this->server->on('Close', [$this, 'onClose']);
    17. //启动服务器
    18. $this->server->start();
    19. }
    20. public function onConnect($server, $fd)
    21. {
    22. echo "客户端id: {$fd}连接.\n";
    23. }
    24. public function onReceive($server, $fd, $reactor_id, $data)
    25. {
    26. $server->send($fd, "发送的数据: {$data}");
    27. }
    28. public function onClose($server, $fd)
    29. {
    30. echo "客户端id: {$fd}关闭.\n";
    31. }
    32. }
    33. new TCP();

    运行:

    2. 客户端:

    1. use Swoole\Coroutine\Client;
    2. use function Swoole\Coroutine\run;
    3. run(function () {
    4. $client = new Client(SWOOLE_SOCK_TCP);
    5. if (!$client->connect('127.0.0.1', 9501, 0.5)) {
    6. echo "connect failed. Error: {$client->errCode}\n";
    7. }
    8. fwrite(STDOUT, '请输入');
    9. $res = fgets(STDIN);
    10. $client->send($res);
    11. echo $client->recv();
    12. $client->close();
    13. });

    运行: 

     

  • 相关阅读:
    前端面试题汇总(vue+html基础)新最最全
    [云原生] K8s之pod控制器详解
    韵搜坊 -- 聚合接口优化(设计模式)
    多线程(基础) - 4万字总结
    30 天 Pandas 挑战 Day16:reset_index()将结果从 Series转为DataFrame
    spring---第六篇
    机器人工程考研难易主观感受和客观数据
    Rust常用特型之TryFrom和TryInto特型
    2310C++构造对象
    计算机体系结构复习笔记
  • 原文地址:https://blog.csdn.net/qq_32450471/article/details/132579131