zoukankan      html  css  js  c++  java
  • 在laravel5.8中集成swoole组件----用协程实现的服务端和客户端(一)

    注意,这种风格的服务端需要swoole4.4以上,这种风格的服务端需要swoole4.4以上,这种风格的服务端需要swoole4.4以上,重要的事情说三遍!!!

    • 服务端
    <?php
    //namespace Swoole;
    use SwooleCoroutineServer;
    use SwooleCoroutineServerConnection;
    go(function(){
        $serv = new Server('127.0.0.1',9508,0.5);
        $serv->handle(function(Connection $conn) use($serv){
            while(true)
            {
                $data = $conn->recv();
                $jdata = json_decode($data,true);
                $conn->send($data);
            }
        });
        $serv->start();
    });
    ?>
    • 客户端
    <?php
    namespace swoole;
    go(function (){
        $client = new coroutineclient(SWOOLE_SOCK_TCP);
        if($client->connect('127.0.0.1',9508,0.5))
        {
            $client->send("来了个菲律宾搏击教练?日本的要吗
    ");
            echo $client->recv();
        }
        else
        {
            echo "General,the nuclear reactor is melting now";
        }
        if($client->close())
        {
            echo "感谢使用";
        }
    });

    上述代码均在laravel5.8中实现

    然后先运行服务端代码,再运行客户端代码

    输出结果:

    以为这样就完了?I‘m deeply sorry,请看下篇 

  • 相关阅读:
    Python学习笔记5
    Python字符串的encode与decode
    python代码`if not x:` 和`if x is not None:`和`if not x is None:`
    关于sys.argv
    Python学习笔记4
    Python学习笔记3
    Python学习笔记2
    生产者消费者_测试
    进程管理
    软件包管理
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/11238921.html
Copyright © 2011-2022 走看看