>백엔드 개발 >PHP 튜토리얼 >Swoole 初识,swoole初识_PHP教程

Swoole 初识,swoole初识_PHP教程

WBOY
WBOY원래의
2016-07-12 08:55:34927검색

Swoole 初识,swoole初识

官方定义:

Swoole:重新定义PHP

PHP的异步、并行、高性能网络通信引擎,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据库连接池,AsyncTask,消息队列,毫秒定时器,异步文件读写,异步DNS查询。 Swoole内置了Http/WebSocket服务器端/客户端、Http2.0服务器端。
Swoole可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网、车联网、智能家居等领域。 使用PHP+Swoole作为网络通信框架,可以使企业IT研发团队的效率大大提升,更加专注于开发创新产品。

 

swoole 扩展安装及案例来源:http://wiki.swoole.com/wiki/page/6.html

 

简单案例:

<?<span>php

</span><span>class</span><span> Server
{

    </span><span>private</span> <span>$serv</span><span>;

    </span><span>public</span> <span>function</span><span> __construct()
    {
        </span><span>$this</span>->serv = <span>new</span> swoole_server("0.0.0.0", 9501<span>);
        </span><span>$this</span>->serv->set(<span>array</span><span>(
            </span>'worker_num' => 8,
            'daemonize' => <span>false</span>,
            'max_request' => 10000,
            'dispatch_mode' => 2,
            'debug_mode' => 1<span>
        ));

        </span><span>$this</span>->serv->on('Start', <span>array</span>(<span>$this</span>, 'onStart'<span>));
        </span><span>$this</span>->serv->on('Connect', <span>array</span>(<span>$this</span>, 'onConnect'<span>));
        </span><span>$this</span>->serv->on('Receive', <span>array</span>(<span>$this</span>, 'onReceive'<span>));
        </span><span>$this</span>->serv->on('Close', <span>array</span>(<span>$this</span>, 'onClose'<span>));

        </span><span>$this</span>->serv-><span>start();
    }

    </span><span>public</span> <span>function</span> onStart(<span>$serv</span><span>)
    {
        </span><span>echo</span> "Start\n"<span>;
    }

    </span><span>public</span> <span>function</span> onConnect(<span>$serv</span>, <span>$fd</span>, <span>$from_id</span><span>)
    {
        </span><span>$serv</span>->send(<span>$fd</span>, "Hello {<span>$fd</span>}!"<span>);
    }

    </span><span>public</span> <span>function</span> onReceive(swoole_server <span>$serv</span>, <span>$fd</span>, <span>$from_id</span>, <span>$data</span><span>)
    {
        </span><span>echo</span> "Get Message From Client {<span>$fd</span>}:{<span>$data</span>}\n"<span>;
    }

    </span><span>public</span> <span>function</span> onClose(<span>$serv</span>, <span>$fd</span>, <span>$from_id</span><span>)
    {
        </span><span>echo</span> "Client {<span>$fd</span>} close connection\n"<span>;
    }

}

</span><span>//</span><span> 启动服务器</span>
<span>$server</span> = <span>new</span> Server();

 

<?<span>php

</span><span>class</span><span> Client
{

    </span><span>private</span> <span>$client</span><span>;

    </span><span>public</span> <span>function</span><span> __construct()
    {
        </span><span>$this</span>->client = <span>new</span><span> swoole_client(SWOOLE_SOCK_TCP);
    }

    </span><span>public</span> <span>function</span><span> connect()
    {
        </span><span>if</span> (!<span>$this</span>->client->connect("127.0.0.1", 9501, 1<span>)) {
            </span><span>echo</span> "Error: {<span>$fp</span>->errMsg}[{<span>$fp</span>->errCode}]\n"<span>;
        }
        </span><span>$message</span> = <span>$this</span>->client-><span>recv();
        </span><span>echo</span> "Get Message From Server:{<span>$message</span>}\n"<span>;

        </span><span>fwrite</span>(STDOUT, "请输入消息:"<span>);
        </span><span>$msg</span> = <span>trim</span>(<span>fgets</span><span>(STDIN));
        </span><span>$this</span>->client->send(<span>$msg</span><span>);
    }

}

</span><span>$client</span> = <span>new</span><span> Client();
</span><span>$client</span>->connect();

 

分别打开两个终端输入:php server.php  php client.php 即可看到效果!

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1116382.htmlTechArticleSwoole 初识,swoole初识 官方定义: Swoole:重新定义PHP PHP的异步、并行、高性能网络通信引擎,使用纯C语言编写,提供了PHP语言的异步多线...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.