在laravel5.8中集成swoole组件----用协程实现的服务端和客户端(一)

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

  • 服务端
<?php
//namespace Swoole;
use Swoole\Coroutine\Server;
use Swoole\Coroutine\Server\Connection;
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 coroutine\client(SWOOLE_SOCK_TCP);
    if($client->connect('127.0.0.1',9508,0.5))
    {
        $client->send("来了个菲律宾搏击教练?日本的要吗\n");
        echo $client->recv();
    }
    else
    {
        echo "General,the nuclear reactor is melting now";
    }
    if($client->close())
    {
        echo "感谢使用";
    }
});

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

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

输出结果:

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

posted @ 2019-07-24 16:26  saintdingtheGreat  阅读(666)  评论(0编辑  收藏  举报